2
我有一个包含许多孩子如iframe
,frameset
,frame
等如何在包含iframe,frame,frameset的页面中通过id获取元素?
我想喜欢document.getElementById(targetId)
函数返回一个元素,但也搜索在所有帧中的文档的页面。
我更喜欢不需要任何扩展如jQuery的解决方案。
我有一个包含许多孩子如iframe
,frameset
,frame
等如何在包含iframe,frame,frameset的页面中通过id获取元素?
我想喜欢document.getElementById(targetId)
函数返回一个元素,但也搜索在所有帧中的文档的页面。
我更喜欢不需要任何扩展如jQuery的解决方案。
你可以做
my_iframe_reference.contentDocument.getElementById(targetId)
通过ID在单个帧中进行搜索。
如果要搜索所有帧,可以遍历window.frames
阵列。
for(var i=0; i<window.frames.length; i++){
var node = window.frames[i].contentDocument.getElementById('id');
if(node) return node;
}
谢谢,我测试了它,但得到这个错误'contentDocument是undefined'。我的浏览器是ie8 – ahoo
尝试'contentWindow.document'而不是:http://stackoverflow.com/questions/1477547/getelementbyid-contentdocument-error-in-ie – hugomg
frames []只返回一个'iframes'数组没有'frames' ,至少在IE中 – ahoo