我使用JS作为更改我正在创建的SPA的内容的一种方式.当我按下按钮更改内容时,HTML将从以下位置更改:

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

对此,请执行以下操作:

<div id="selectDiv" style>

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.

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.
}

有什么建议吗?

推荐答案

You can use .is(':visible')

Selects all elements that are visible.

例如:

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

此外,您还可以通过以下方式获取可见的div:

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

Live example:

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相关问答推荐

JQuery DateTimePicker-在 Select 中抓取星期几

在添加_renderMenu方法时,是什么原因导致jQuery UI AutoComplete抛出TypeError?

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

Sheets从多张sheet中导入range匹配数据

在 Bootstrap 3 中向工具提示添加换行符

如何 Select 具有特定文本的所有锚标记

JQuery/Javascript:判断 var 是否存在

什么是不使用 jQuery 的经验技术原因?

使用 JQuery 清除下拉列表

jQuery获取表单字段值

如何在 HTML选项标签上显示工具提示?

如何使用 jQuery 进行带命名空间的 XML 解析

jquery 从特定表单获取所有输入

使用 jQuery 按字母顺序对选项元素进行排序

将图像 url 转换为 Base64

使用 jquery 通过文本设置下拉值

jquery可排序占位符高度问题

jQuery:如何找到父母的特定子元素?

如何使锚链接不可点击或禁用?

上传文件前验证文件扩展名