可以得到document
的节点<iframe>
的对象与 contentDocument
HTMLIFrameElement
的属性(property)
但我找不到获取 <iframe>
的方法退出节点。
是 <iframe>
之间的关系它的文档只有一种方式?
如果是,为什么?
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"
最佳答案
iframe
元素与 div
元素(它是iframe 页面)。
您可以像这样获取 iframe
元素:
iframe.contentWindow.frameElement === iframe
或者在 iframe
脚本环境中:
window.frameElement
或者使用div
div.ownerDocument.defaultView.frameElement === iframe
关于javascript - <iframe> 和它的文档之间的关系是一种方式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249702/