jQuery visibili...LOGIN

jQuery visibility filter

Visibility Filter
The visibility filter selects the corresponding elements based on their visibility and invisibility.

Filter name jQuery syntaxDescription Return
:hidden $(':hidden')Select all invisible elements Collection elements
:visible $(':visible')Select all visible elements Collection elements

$('p:hidden).size(); //The hidden element of element p

$('p:visible').size(); //The displayed element of element p

Note: The :hidden filter generally contains elements whose CSS style is display:none, input form type is type="hidden" and visibility:hidden.

Example
jQuery's visibility selector selects the corresponding elements based on the visible and invisible states of the elements. There are two main ones: visible and invisible: hidden. Today we will mainly learn how to use these two selectors. Let’s first look at an HTML structure to facilitate learning the use of these two selectors:

<div class="wrap">
 <span></span>
 <div></div>
 <div style="display:none">Hider!</div>
 <div style="visibility:hidden">Hider!</div>
 <div class="startHidden">Hider!</div>
 <div class="startVisibilityHidden">Hider!</div>
 <div></div>
 <form>
  <input type="hidden" />
  <input type="hidden" />
  <input type="hidden" />
 </form>
 <span></span>
 <button>显示隐然元素</button>
</div>

CSS

<style type="text/css">
 .wrap {
   width: 500px;
   padding: 10px;
   margin: 20px auto;
   border: 1px solid #ccc;
 }
  
 .wrap div {
  width: 70px;
  height: 40px;
  background: #0083C1;
  margin: 5px;
  float: left
 }
 
 span {
  display: block;
  clear: left;
  color: #008000;
 }
 
 .startHidden {
  display: none;
 }
 
 .startVisibilityHidden {
  visibility: hidden;
 }
</style>

Initial effect

2016041920020736.png

Below Let’s take a look at the syntax, usage rules and functions of these two selectors respectively

1. Invisibility selector: :hidden

Selector

$("E:hidden") //E represents the element tag

or

$(":hidden") //Select all hidden elements

Description:

E:hidden means selecting hidden E elements, while :hidden means selecting all invisible elements

Return value:

Collection elements

Example:

$(document).ready(function(){
$("span:first").text("Found " + $(":hidden", document.body).length + "hidden elements total.");//Add text to the first span tag to show how many elements are hidden in the body
$("div:hidden").show(" 3000");//Show all hidden div elements
$("span:last").text("Found " + $("input:hidden").length + " hidden inputs");//In Add text to the last span tag to show how many inputs are hidden
});

Function:

":hidden" selects all invisible elements, and some browse The container also includes all tags within <header>, and the invisible elements referred to here are the two styles of "display:none" and form "type="hidden"", and do not include hidden elements with "visibility:hidden" element. In addition, I would like to remind everyone that ":hidden" will sometimes cause all tags in the <header> to be selected, so it is recommended to add an element tag in front.

Effect:

1.png

2. Visibility selector: :visible

Selector:

$(" E:visible") //E refers to the element tag, select the specified visible element tag

or

$(":visible") //Select all visible elements Element

Description:

"E:visible" means selecting a visible E element, for example, "$("div:visible")" means selecting All visible div elements, and "$(":visible")" means selecting all visible elements

Return value:

Collection elements

Instance:

<script type="text/javascript">
$(document).ready(function(){
$("div:visible").click(function(e){ // Bind a click event to the visible DIV element
$(this).css("border","2px solid red"); //Add a 2px red border to the visible DIV element
e.stopPropagation( );//Stop event bubbling
});
$("button").click(function(e){ //Bind a click event to button
$("div:hidden ").show("fast").css("background","red");//The hidden elements are displayed and the background turns red
e.stopPropagation();//Stop event bubbling
});
});
</script>

Function:

The first piece of code above is the DIV element visible by mouse click Finally, the element will add a 2px red border style, and the second piece of code is that clicking the button will display all hidden elements and add a red background color. The visible elements referred to here are the same as the hidden elements we mentioned earlier, except that they are not hidden by "display:none" or ".hide()".

Effect:


2.png

Finally one more thing: ":visible" filters out all visible elements, but Visible here refers to elements that are not hidden by "display:none" or using the ".hide()" function; ":hidden" selects all hidden elements. Similarly, the so-called hidden here does not refer to "visibility: hidden", but to the form element of "display:none" or "type="hidden"".

This is a brief introduction to jQuery’s visibility filter selector. Interested friends can test it locally, which may enhance their understanding of them.


Next Section
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>05-可见性过滤选择器.html</title> <!-- 引入jQuery --> <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script> <link rel="stylesheet" type="text/css" href="./css/style.css" /> <script type="text/javascript"> $(document).ready(function(){ //<input type="button" value=" 选取所有可见的div元素" id="b1"/> $("#b1").click(function(){ $("div:visible").css("background","red"); }); //<input type="button" value=" 选取所有不可见的元素, 利用 jQuery 中的 show() 方法将它们显示出来" id="b2"/> $("#b2").click(function(){ $("div:hidden").show(1000); }); //<input type="button" value=" 选取所有的文本隐藏域, 并打印它们的值" id="b3"/> $("#b3").click(function(){ var $input=$("input:hidden"); //显示迭代.. // for(var i=0;i<$input.length;i++){ // $inputp[i] // // } //隐式迭代 $input.each(function(index,doc){ //alert(index) //alert(doc.value); // doc.val(); // 转换成jQuery 对象在调用 alert($(doc).val()); }) }); }); </script> </head> <body> <h3>可见性过滤选择器.</h3> <button id="reset">手动重置页面元素</button> <input type="checkbox" id="isreset" checked="checked"/><label for="isreset">点击下列按钮时先自动重置页面</label> <br/><br/> <input type="button" value=" 选取所有可见的div元素" id="b1"/> <input type="button" value=" 选取所有不可见的元素, 利用 jQuery 中的 show() 方法将它们显示出来" id="b2"/> <input type="button" value=" 选取所有的文本隐藏域, 并打印它们的值" id="b3"/> <br /><br /> <div class="one" id="one" > id为one,class为one的div <div class="mini">class为mini</div> </div> <div class="one" id="two" title="test" > id为two,class为one,title为test的div. <div class="mini" title="other">class为mini,title为other</div> <div class="mini" title="test">class为mini,title为test</div> </div> <div class="one"> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini">class为mini</div> <div class="mini" title="tesst">class为mini,title为tesst</div> </div> <input type="hidden" value="hidden_1"> <input type="hidden" value="hidden_2"> <input type="hidden" value="hidden_3"> <input type="hidden" value="hidden_4"> <div style="display:none;" class="none">style的display为"none"的div</div> <div class="hide">class为"hide"的div</div> <span id="mover">正在执行动画的span元素.</span> <!-- Resources from http://down.liehuo.net --> </body> </html>
submitReset Code
ChapterCourseware