我有一个页面,其中包含许多子元素,例如 iframe
、frameset
、frame
等。
我想要一个像 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;
}
关于javascript - 如何在包含 iframe、frame、frameset 的页面中通过 id 获取元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7060156/