【代码片段】jQuery测试可见性过滤选择器

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>第2章示例5</title>
 6 <style type="text/css">
 7     body { width:760px; }
 8     div,p,h3,h1 { border:4px solid black; background-color:green; color:white; margin:6px; padding:5px; font:bold 14px/1 Arial,Helvetica,sans-serif; width:220px; float:left; }
 9     div p { width:205px; border-width:2px; margin:5px 0; float:none; }
10     h1 { margin:6px 256px; }  h3 { position:relative; margin-right:500px; }
11     div.top { height:65px; }
12     .clear { clear:both; }
13     div.hide { display:none; }  p.hide { visibility:hidden; }
14     .highlight { background-color:gold; color:black; }
15     form { clear:both; }
16     button { font:bold 16px/1 Arial,Helvetica,sans-serif; margin:1px 3px; padding:2px; cursor:pointer; width:240px; }
17 </style>
18 <script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
19 <script type="text/javascript">
20     $(document).ready(function(){
21         $("#btn1").click(function(){alert("不可见的p元素有:"+$("p:hidden").length+"");});    
22         $("#btn2").click(function(){alert("可见的div元素有:"+$("div:visible").length+"");});    
23         function swing() {
24             $("h3").animate({left:"500"},7000)
25                     .animate({left:"0"},7000, swing);
26         }
27         swing();                            
28     });
29 </script>
30 </head>
31 <body>
32     <h1>&lt;h1&gt; &lt;/h1&gt;</h1>    
33     <div class="clear top">
34         &lt;div class="clear top"&gt;
35         <p>&lt;p&gt;hello&lt;/p&gt;</p>
36         &lt;/div&gt;
37     </div>
38     <div class="top" id="core">
39         &lt;div class="top" id="core"&gt;
40         <p>&lt;p&gt; &lt;/p&gt;</p>
41         &lt;/div&gt;
42     </div>
43     <div class="top">
44         &lt;div class="top"&gt;<br/>
45         hello, John<br/>
46         &lt;/div&gt;
47     </div>        
48     <p class="clear">&lt;p class="clear"&gt; &lt;/p&gt;</p>
49     <p>&lt;p &gt; &lt;/p&gt;</p>
50     <p>&lt;p &gt; &lt;/p&gt;</p>    
51     <h3 class="clear">&lt;h3 class="clear"&gt; &lt;/h3&gt;</h3>    
52     <p class="clear">&lt;p class="clear"&gt; &lt;/p&gt;</p>
53     <p>&lt;p &gt; &lt;/p&gt;</p>
54     <p class="hide">&lt;p class="hide"&gt; &lt;/p&gt;</p>
55     <div class="clear bottom">
56         &lt;div class="clear bottom"&gt;
57         <p>&lt;p&gt; &lt;/p&gt;</p>
58         <p>&lt;p&gt; &lt;/p&gt;</p>
59         <p>&lt;p&gt; &lt;/p&gt;</p>
60         &lt;/div&gt;
61     </div>
62     <div class="bottom">
63         &lt;div class="bottom"&gt;
64         <p>&lt;p&gt; &lt;/p&gt;</p>
65         <p>&lt;p&gt; &lt;/p&gt;</p>
66         <p>&lt;p&gt; &lt;/p&gt;</p>        
67         &lt;/div&gt;
68     </div>
69     <div class="hide bottom">
70         &lt;div class="hide bottom"&gt;
71         <p>&lt;p&gt; &lt;/p&gt;</p>
72         <p>&lt;p&gt; &lt;/p&gt;</p>
73         <p>&lt;p&gt; &lt;/p&gt;</p>        
74         &lt;/div&gt;
75     </div>
76     <form>
77         <button type="button" id="btn1">$("p:hidden")</button>
78         <button type="button" id="btn2">$("div:visible")</button>
79     </form>
80 </body>
81 </html>

原文地址:https://www.cnblogs.com/kojya/p/2944806.html