简体   繁体   English

如何找出元素是否在隐藏的iframe中

[英]How to find out whether an element is in a hidden iframe

Using jQuery, one can easily find out whether a particular element is visible using 使用jQuery,您可以轻松地通过使用jQuery查找特定元素是否可见

$(element).is(':visible')

While having some limitations (doesn't cover css visibility hidden or the actual visibility in the viewport, ie whether it's covered by other elements or scrolled away), I find it being useful for my scenario. 虽然有一些限制(没有隐藏css可见性或视口中的实际可见性,即它是否被其他元素覆盖或滚动了),但我发现它对我的场景很有用。 The catch is, it only works within one iframe. 问题是,它只能在一个iframe中使用。

If the element has any parent within its document with display:none; 如果元素在其文档中具有display:none;任何父元素display:none; , it returns false. ,它返回false。 If the whole document is included in an iframe which has display:none , it returns true. 如果整个文档包含在具有display:none的iframe中,则返回true。 Is it possible to somehow detect this in another way ? 是否有可能以其他方式检测到这一点?

Hmm, seems like you have to call top (parent) document within an iframe, then search for specific iframe and check if it's visible. 嗯,好像您必须在iframe中调用顶级(父)文档,然后搜索特定的iframe并检查它是否可见。

You'll probably have to have same domain/subdomain for this to work, but anyway: 您可能必须具有相同的域/子域才能运行,但是无论如何:

Let's assume you know iframe id/class (you have to). 假设您知道iframe ID /类(必须)。

if ($(top).find('#iframeID').is(':visible')) {
  // iframe is visible
} else {
 // iframe is NOT visible
}

Can't guarantee correct work. 无法保证正确的工作。

似乎该窗口的frameElement属性可在所有浏览器中使用,并提供包含该窗口的当前iframe(如果不适用跨域限制,我就是这种情况)。

not possible afaik. 不可能afaik。 an iframe is basically a different site and as such guarded against xss. iframe基本上是一个不同的站点,因此要防范xss。 simply don't use iframes but server-side includes. 根本不使用iframe,而是使用服务器端包含。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM