是< iframe>之间的关系它的文档是一种方式吗? [英] Is the relation between <iframe> and it's document one way?
问题描述
您可以获取文档
对象的< iframe>
的节点与 contentDocument
属性的 HTMLIFrameElement
You can get the nodes of the document
object of an <iframe>
with the contentDocument
property of the HTMLIFrameElement
但是我找不到一种获取< iframe>
退出节点。
< iframe>
之间的关系,它的文档只是一种方式?
如果是,为什么?
But I can't find a way getting the <iframe>
back out of the node.
Is the relation between the <iframe>
and it's document is only one way?
If it does, WHY?
var iframe = document.getElementById('iframe');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var div = doc.getElementsByTagName('div')[0];
console.log('Did we find the iframe? ' + ($(div).closest('iframe').length > 0));
// Output: "Did we find the iframe? false"
推荐答案
code> iframe 元素位于不同的文档
(主页中的元素)比 div
元素(它是iframe页面中的一个元素)。
The iframe
element is in different document
(it's an element in the main page) than the div
element (it's an element in the iframe page).
您可以这样获取 iframe
元素:
iframe.contentWindow.frameElement === iframe
或者在 iframe内
脚本环境:
window.frameElement
或使用 div
div.ownerDocument.defaultView。 frameElement === iframe
这篇关于是< iframe>之间的关系它的文档是一种方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!