Home >Web Front-end >JS Tutorial >How to determine whether an element is hidden in jquery

How to determine whether an element is hidden in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-24 12:06:072853browse

Jquery method to determine whether an element is hidden: 1. Use CSS attributes, the code is [var display =$('#id').css('display');]; 2. Use jquery's built-in selector , the code is [if($("#test").is(":hidden")].

How to determine whether an element is hidden in jquery

The operating environment of this tutorial: windows7 system, jquery3 .2.1 version, DELL G3 computer.

Recommendation: jquery video tutorial

jquery method to determine whether an element is hidden:

1: Use CSS attributes

The code is as follows:

var display =$('#id').css('display');
if(display == 'none'){
   alert("被你发现了,我是隐藏的啦!");
}

2: Use jquery built-in selector

Assume our page There is such a label,

The code is as follows:

<div id="test">
<p>仅仅是测试所用</p>
</div>

Then, we can use the following statement to determine whether the label with the id "test" is hidden:

The code is as follows:

if($("#test").is(":hidden")){...} //前提是已经将jQuery的库导进来了

In this way, we can easily determine whether an element is hidden and set animations according to its status. For example:

The code is as follows:

if($("#test").is(":hidden")){
       $("#test").show();    //如果元素为隐藏,则将它显现
}else{
      $("#test").hide();     //如果元素为显现,则将其隐藏
}

3 : jQuery determines whether the element is displayed or hidden

The code is as follows:

var node=$(&#39;#id&#39;);

The first way of writing

The code is as follows:

if(node.is(&#39;:hidden&#39;)){  //如果node是隐藏的则显示node元素,否则隐藏
  node.show(); 
}else{
  node.hide();
}

The code for the second way of writing

is as follows:

if(!node.is(&#39;:visible&#39;)){  //如果node是隐藏的则显示node元素,否则隐藏
  node.show(); 
}else{
  node.hide();
}
if(node.is(&#39;:visible&#39;)){  //如果node是显示的则隐藏node元素,否则显示
  node.hide();
}else{
  node.show();
}

Related free learning recommendations: javascript (video)

The above is the detailed content of How to determine whether an element is hidden in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn