我有两个文档,其中一个用iframe嵌入到另一个文档中。我正在尝试使用此代码访问iframe中的img标记,但是,我无法在iframe元素上使用document.getElementById函数:
iframes = document.getElementsByTagName("iframe")
spaces = iframes[0].contentWindow.document;
spaces = spaces.getElementsByTagName("img")
for (var i=0, max=spaces.length; i < max; i++) {
alert(spaces[i].innerHTML)
}
alert(spaces)
var x = document.getElementById("frame");
var y = (x.contentWindow || x.contentDocument);
if (y.document)y = y.document;
alert(y.body.innerHTML)
spaces返回[object HTMLCollection],y返回[object HTMLDocument]
答案 0 :(得分:0)
因此,如果您与iframe内容位于同一个域中,则可以使用
document.getElementById('my-iframe').contentWindow.document.getElementById(...)
如果您不在同一个域中,出于安全原因,您无法访问/操作iframe内容。否则,恶意的人可能会抛出一个看似Facebook或其他东西的iframe,并窃取其他民间信息。
如果您不在同一个域中但拥有您正在使用的两个域,则可以在两个帧之间设置messaging。并使用父母向孩子发送信息/操纵信息。