对于 iframe(甚至旧的“frame”元素),“contentDocument”到底代表什么?它等同于“html”元素或“body”元素吗? 它有什么用? 所有浏览器都支持此属性吗?
最佳答案
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.
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.
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/