如何检查元素是否在iframe中 [英] How to check if an element is inside an iframe or not

查看:361
本文介绍了如何检查元素是否在iframe中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个DOM节点,你想知道它是否位于iframe中。一种方法是检查它的父链,看看你是否在到达父窗口之前到达了一个iframe。然而,我想知道是否有更快的方法来做到这一点。 您可以检查 ownerDocument 属性节点:$ b​​
$ b

  if(node.ownerDocument!== document){
//节点必须在iframe内
}


Let's say you have a DOM node and you want to know whether it is located inside an iframe or not. One way would be to check it's parent chain to see if you reached an iframe before reaching the parent window. However, I'd like to know if there is a faster way to do this.

解决方案

You could probably check the ownerDocument property of the node:

if(node.ownerDocument !== document) {
    // node must be inside iframe
}

这篇关于如何检查元素是否在iframe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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