JQuery:如果div是可见的 [英] JQuery: if div is visible

查看:84
本文介绍了JQuery:如果div是可见的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JS作为改变我正在创建的SPA内容的一种方式。当我按下按钮更改内容时,HTML会从此更改:

I'm using JS as a way of changing the content of an SPA I'm creating. When I press a button to change the content the HTML changes from this:

<div id="selectDiv" style="display: none;">

到此:

<div id="selectDiv" style>

现在我的SPA的一部分有一个div,其中包含许多复选框,每个复选框代表一个div,以及因此,当我按下提交按钮时,应显示的下一个div将是所选复选框列表中的第一个项目。

Now part of my SPA has a div that contains a number of checkboxes, each representing a div, and so when I press the submit button, the next div that should be displayed will be the first item in the checkbox list that was selected.

我想知道是否有在JQuery中用于几乎检测哪个div现在可见的代码。类似这样的事情:

I'm wondering if there's a way in JQuery for the code to "almost detect" which div is now visible. something like this:

if($('#selectDiv').isVisible()){
    //JS code associated with this div.
}

有什么建议吗?

推荐答案

您可以使用 .is( ':visible')

You can use .is(':visible')


选择所有可见的元素。

Selects all elements that are visible.

例如:

if($('#selectDiv').is(':visible')){

此外,你可以获得div可见:

Also, you can get the div which is visible by:

$('div:visible').callYourFunction();

实例:

console.log($('#selectDiv').is(':visible'));
console.log($('#visibleDiv').is(':visible'));

#selectDiv {
  display: none;  
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="selectDiv"></div>
<div id="visibleDiv"></div>

这篇关于JQuery:如果div是可见的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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