javascript - iframe 的 contentDocument

标签 javascript html dom iframe frames

对于 iframe(甚至旧的“frame”元素),“contentDocument”到底代表什么?它等同于“html”元素或“body”元素吗? 它有什么用? 所有浏览器都支持此属性吗?

最佳答案

w3.org

contentDocument of type Document, readonly, introduced in DOM Level 2 The document this frame contains, if there is any and it is available, or null otherwise.

MDN

From the DOM iframe element, scripts can get access to the window object of the included HTML page via the contentWindow property. The contentDocument property refers to the document element inside the iframe (this is equivalent to contentWindow.document), but is not supported by Internet Explorer versions before IE8.

msdn

the document this page or frame contains
This property is new in Windows Internet Explorer 8

因此,要获取 body 元素的 innerHTML,您可以使用

iframe.contentDocument.getElementsByTagName("body")[0]

iframe.contentDocument.body

在当今的浏览器中。

关于javascript - iframe 的 contentDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581803/

相关文章:

javascript - Chartjs-Vue 图表为空

html - 带有固定导航栏的粘性页脚

jquery - 计算同一类的连续元素,直到出现另一个类

javascript - 使用向上/向下按钮更新我的 HTML5 slider

javascript - 如何在 Spotify 中从特定时间开始播放歌曲

javascript - 所有页面都出现 float 窗口

javascript - 如何在不影响 DOM 的情况下获取 HTML 的克隆、修改、获取 HTML 字符串?

javascript - 我如何模拟文本字段中的退格操作?

javascript - 为什么 .next() 给我 'undefined'

javascript - offset().top 给出了错误的值