使用DOM访问iframe [英] access iframe with DOM
问题描述
在下面的代码中,警报可以正常工作并按应有的要求打印"DIV:IFRAME",但随后它说cNs [1] .childNodes [1] .document没有属性.
In the following code, the alert works fine and prints "DIV : IFRAME" as it should however it then says that cNs[1].childNodes[1].document has no properties.
HTML:
<div id="WinContainer">
<div style="display: none;"><iframe id="frame1" name="frame1"></iframe></div>
<div style="display: none;"><iframe id="frame2" name="frame2"></iframe></div>
</div>
JavaScript:
JavaScript:
var cNs = document.getElementById('WinContainer').childNodes;
alert(cNs[1].tagName + ' : ' + cNs[1].childNodes[1].tagName);
cNs[1].childNodes[1].document.location = 'someurl.pl';
但是,如果我这样做:
frame1.document.location = 'someurl.pl';
它工作正常.
推荐答案
iframe
DOM节点具有一个名为 contentDocument
的属性,该属性与 document等效.
,但针对该 iframe
.
The iframe
DOM node has a property called contentDocument
which will be the equivalent of document
, but for that iframe
.
如果正在显示的页面在另一台服务器上(甚至在同一台服务器上的其他端口上),您将在尝试访问该页面时遇到安全异常.
If the page being displayed is on another server tho (or even on a different port on the same server) you will get a security exception trying to access it.
不确定这是否适用于IE.
Not sure if this works for IE.
这篇关于使用DOM访问iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!