如何将元素附加到 iframe 窗体

How to attach a element to an iframe form?

本文关键字:iframe 窗体 元素      更新时间:2023-09-26

这是我的iframe代码

<iframe id='SoapExeFrame' src='' style='display:none'>
<form id='soapdatas' action='ps_dlgwnd_3.jsp' method="post">
</form>
</iframe>

我想在将请求提交到目标 jsp 页面之前附加一些表单元素,所以我写了下面的代码

var oInput = SoapExeFrame.document.createElement('INPUT');
oInput.name ="webservice/webservice";
oInput.value = valueofService;
SoapExeFrame.soapdatas.appendChild(oInput);

但是IE11抛出了一个错误,指出soapdatas不存在。

获取 iFrame 文档与普通文档不同,这就是您可能会遇到问题的原因。

var SoapExeFrame = document.getElementById('SoapExeFrame').contentDocument || document.getElementById('SoapExeFrame').contentWindow.document;
var oInput = SoapExeFrame.createElement('input');
oInput.name ="webservice/webservice";
oInput.value = valueofservice;
    //Append to the form
SoapExeFrame.getElementById('soapdatas').appendChild(oInput);

我在 http://codepen.io 运行了这个,我发现 iFrame 不包含表单,这是您可能无法使用它的另一个原因,因为如果 iFrame 在该浏览器中不起作用,它中的数据(我相信),您需要先对其进行排序,然后 javascript 将起作用。