1. Visibility filter selector
:hidden selects all invisible elements. For example:$(":hidden") means to select all invisible elements. Includes:
<input type="hidden" /> <div style="display:none;"></div> <div style="visibity:hidden"></div>
Annotation content on the html page /**/,
, (if this tag is on the page),<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试jQuery选择器</title> <script src="Scripts/jquery-1.7.1.js"></script> <style type="text/css"> .test { background-color: yellow; } </style> </head> <body> <input type="hidden" value="hidden"/> <div style="display:none"></div> <script type="text/javascript"> var $hidden1 = $(":hidden"); var $hidden2 = $("input:hidden"); var len1 = $hidden1.length; var len2 = $hidden2.length; console.log("$(':hidden').length:"+len1); console.log("$('input:hidden').lengh:" + len2); $.each($hidden1, function () { console.log(this.nodeName+":"+ this.innerHTML); }); $.each($hidden2, function () { console.log(this.nodeName + ":" + this.innerHTML); }); </script> </body> </html>
The output result is as follows:
$(':hidden').length:8 $('input:hidden').lengh:1 HEAD: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>测试jQuery选择器</title> <script src="Scripts/jquery-1.7.1.js"></script> <style type="text/css"> .test { background-color: yellow; } </style> META: TITLE:测试jQuery选择器 SCRIPT: STYLE: .test { background-color: yellow; } INPUT: DIV: SCRIPT: var $hidden1 = $(":hidden"); var $hidden2 = $("input:hidden"); var len1 = $hidden1.length; var len2 = $hidden2.length; console.log("$(':hidden').length:"+len1); console.log("$('input:hidden').lengh:" + len2); $.each($hidden1, function () { console.log(this.nodeName+":"+ this.innerHTML); }); $.each($hidden2, function () { console.log(this.nodeName + ":" + this.innerHTML); }); INPUT:
2. Whether there are spaces in the selector
The spaces in the selector cannot be ignored. The presence or absence of spaces will get different results. Look at the example below:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试jQuery选择器</title> <script src="Scripts/jquery-1.7.1.js"></script> <style type="text/css"> .test { background-color: yellow; } </style> </head> <body> <div class="test"> <div style="display:none">a</div> <div style="display:none">b</div> <div style="display:none">c</div> <div class="test" style="display:none">d</div> </div> <div class="test" style="display:none;">e</div> <div class="test" style="display:none">f</div> <script type="text/javascript"> var $t1 = $(".test :hidden");//带空格 var $t2 = $(".test:hidden");//不带空格 var len1 = $t1.length; var len2 = $t2.length; console.log("$('.test :hidden').length:" + len1); console.log("$('.test:hidden').length:" + len2); $.each($t1, function () { console.log(this.nodeName+":"+ this.innerHTML); }); console.log(); $.each($t2, function () { console.log(this.nodeName + ":" + this.innerHTML); }); </script> </body> </html>
The output result is as follows:
$('.test :hidden').length:4 $('.test:hidden').length:3 DIV:a DIV:b DIV:c DIV:d DIV:d DIV:e DIV:f
The reason why there are different results is because the descendant selector and filter selector are different.
$(".test :hidden") with spaces selects the hidden elements in the element with class "test", which is equivalent to .class *:hidden and is similar to the expression of css.
$(".test:hidden") without spaces selects the hidden element whose class is "test".