是< iframe>之间的关系它的文档是一种方式吗? [英] Is the relation between <iframe> and it's document one way?

查看:175
本文介绍了是< iframe>之间的关系它的文档是一种方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以获取文档对象的< 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?

一个非工作的DEMO:

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

更新演示

这篇关于是&lt; iframe&gt;之间的关系它的文档是一种方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆