javascript - 如何在包含 iframe、frame、frameset 的页面中通过 id 获取元素?

标签 javascript frame

我有一个页面,其中包含许多子元素,例如 iframeframesetframe 等。

我想要一个像 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/

相关文章:

iphone - 在 iOS 上,更改 UIView 对象的框架和中心可以改变其内部坐标系吗?

python - 如何从另一个框架控制一个框架?

iphone - 拖动时展开 View

JavaScript 或 jquery : find out weather a page have openned with internet explorer object or not?

javascript - Angular 形式禁用对 valueChanges 的控制

uibutton - Swift iOS-如何将按钮添加到 CollectionView 单元格外部

javascript - 可以防止您的网站在另一个域的框架集和 IFrame 中运行吗?

javascript - 类型错误 : Class extends value undefined is not a constructor or null in react js

javascript - 获取页面矩形区域内的 DOM 元素

javascript - 不使用按钮或 A 标签触发 Bootstrap Collapse 的方法?