如何使用jQuery检查IFrame在页面上是否可见 [英] How to check if IFrame is visible on page using jQuery

查看:360
本文介绍了如何使用jQuery检查IFrame在页面上是否可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含多个iframe的页面:

Suppose I have a page with multiple IFrames :

主页

<div id='someDiv1' style='display:none; '>
       <iframe id='iframe1' src='iframe1.html'>
           <input id='someinput'></input>
       </iframe>
</div>

iframe(iframe1.html)

<input id='someinput'></input>
<script>
  function isElementVisible(elem){

  }
</script>

在这种情况下,如何检查由于IFrame的父div隐藏了该元素而该元素是否可见/隐藏?

In this scenario how do i check if the element is visible/hidden due to the parent div of IFrame hiding it?

我尝试使用 $('#someinput').is(':visible'),但是如果我在IFrame中运行它,我总是会得到正确的结果.我没有选择更改页面结构或在父级内部执行脚本的选项.

I tried using $('#someinput').is(':visible') but I always get true if I run it inside IFrame. I don't have an option to change the page structure nor execute the script inside parent.

推荐答案

iframe中的文档不了解您主页上的情况...但是我相信您可以查询"您的父母来检查是否可见吗?

The document inside your iframe is not aware of the situation in your main page... But I believe you can just 'query' your parent to check if it's visible?

$('#someDiv1', window.parent.document).is(":visible");

或者没有jquery,因为您并不是真的需要它..

or without jquery because you don't really need it..

if(window.parent.document.getElementById("someDiv1").style.display != "none") 
    alert("Visible");
else 
    alert("Hidden");

这篇关于如何使用jQuery检查IFrame在页面上是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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