I'm trying to get access to a form and its elements. The form is within an iframe and the javascript code that is accessing the form is within the main document.
我正在尝试访问一个表单及其元素。表单在iframe中,访问表单的javascript代码在主文档中。
I'm not sure what else I should put into the question, so please let me know if I need to add something else.
我不知道我还应该在这个问题上放些什么,所以如果我需要补充点什么,请告诉我。
(form and main page are in the same domain)
(表格和主页在同一域名)
Thanks
谢谢
2 个解决方案
#1
14
var ifr = document.getElementById( yourIframeId );
var ifrDoc = ifr.contentDocument || ifr.contentWindow.document;
var theForm = ifrDoc.getElementById( yourFormId );
Or you could have some code in the frame that sets a variable in parent
to the form, but I wouldn't trust that method.
或者你可以在框架中使用一些代码将父变量设置为窗体,但我不相信这个方法。
#2
3
If your iframe has a name
attribute, that can be used as a window name. If the frame name is "myframe":
如果您的iframe有一个name属性,可以将其用作窗口名。如果帧名为“myframe”:
myframe.document.getElementById("myform") // gives you the form element
#1
14
var ifr = document.getElementById( yourIframeId );
var ifrDoc = ifr.contentDocument || ifr.contentWindow.document;
var theForm = ifrDoc.getElementById( yourFormId );
Or you could have some code in the frame that sets a variable in parent
to the form, but I wouldn't trust that method.
或者你可以在框架中使用一些代码将父变量设置为窗体,但我不相信这个方法。
#2
3
If your iframe has a name
attribute, that can be used as a window name. If the frame name is "myframe":
如果您的iframe有一个name属性,可以将其用作窗口名。如果帧名为“myframe”:
myframe.document.getElementById("myform") // gives you the form element