现在的位置: 首页 > 综合 > 正文

jQuery中的过滤器

2018年05月20日 ⁄ 综合 ⁄ 共 4321字 ⁄ 字号 评论关闭

1、基本过滤选择器

:first
:last
:not(selector) :selector匹配的节点之外的节点
:even :偶数
:odd :奇数
:eq(index)
:gt(index) :比他大的

:lt(index) :比他小的

  1. <html>  
  2.     <head>  
  3.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>  
  4.         <script>  
  5.             $(function(){  
  6.                 $('#b1').click(function(){  
  7.                     //$('tr:first').css('background-color','#cccccc');  
  8.                     //$('tbody tr:first').css('background-color','#cccccc');  
  9.                     //$('tbody tr:not(#tr2)').css('background-color','#cccccc');  
  10.                     //$('tbody tr:even').css('background-color','#cccccc');  
  11.                     $('tbody tr:eq(2)').css('background-color','#cccccc');  
  12.                 });  
  13.             });  
  14.         </script>  
  15.     </head>  
  16.     <body>  
  17.         <table border="1" cellpadding="0" cellspacing="0" width="60%">  
  18.             <thead>  
  19.                 <tr>  
  20.                     <td>name</td><td>age</td>  
  21.                 </tr>  
  22.             </thead>  
  23.             <tbody>  
  24.                 <tr><td>zs</d><td>22</td></tr>  
  25.                 <tr id="tr2"><td>ls</d><td>22</td></tr>  
  26.                 <tr><td>ww</d><td>22</td></tr>  
  27.                 <tr><td>ll</d><td>22</td></tr>  
  28.             </tbody>  
  29.         </table>  
  30.         <input type="button" value="clickMe" id="b1"/>  
  31.     <body>  
  32. </html>  

2、内容过滤选择器

:contains(text)
:empty:没有子节点的节点或者文本内容为空的节点
:has(selector)
:parent :包含有父节点的节点

  1. <html>  
  2.     <head>  
  3.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>  
  4.         <script>  
  5.             $(function(){  
  6.                 $('#b1').click(function(){  
  7.                     $(':contains(hello)').css('background','red');  
  8.                     //$(':empty').css('','');  
  9.                     //$('div :has(p)').css('','');  
  10.                 });  
  11.             });  
  12.         </script>  
  13.     </head>  
  14.     <body>  
  15.         <div>  
  16.             <div>hello</div>  
  17.             <div>你好</div>  
  18.             <div>  
  19.                 <p>java</p>  
  20.             <div>  
  21.             <input type="button" value="clickMe" id="b1"/>  
  22.         </div>  
  23.     </body>  
  24.   
  25. </html>  

其实我的目的并不是让全部屏幕变成红色,为什么全部变成红色的呢?再看下面代码,我在contains(hell0)前面加一个div

  1. $('div:contains(hello)').css('background','red');  

可以看到虽然不是全屏了,但是还不是我想要的结果,怎么才能只将hello下面的背景变成红色呢?可以这样做

  1. $('div div:contains(hello)').css('background','red');  

3、可见性过滤选择器

:hidden

找到input type="hidden" 以及display=none

:visible

  1. $(function(){  
  2.     $('#a1').click(function(){  
  3.         $('div:hidden').css('display','block');  
  4.         //如过有这个样式则去掉,没有则加上  
  5.         $('#d1').toggleClass('show');  
  6.     });  
  7.     //每点击一次,执行一个函数,循环执行  
  8.     $('#a1').toggle(function(){  
  9.         //$('#d1').css('display','block');  
  10.         $('#d1').show(400); //在400毫秒种打开  
  11.         //或者使用slow fast normal三个参数  
  12.         $('#d1').show(slow);  
  13.     },function(){  
  14.         //$('#d1').css('display','none');  
  15.         $('#d1').hide();  
  16.     });  
  17. });  

4、过滤选择器

(1)属性过滤选择器

[attribute] 
[attribute=value]
[attribute!=value]

  1. <html>  
  2.     <head>  
  3.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>  
  4.         <script>  
  5.             $(function(){  
  6.                 $('#b1').click(function(){  
  7.                     $('p[id=p1]').html('hello java');  
  8.                 });  
  9.             });  
  10.         </script>  
  11.     </head>  
  12.     <body>  
  13.         <p id="p1">hello</p>  
  14.         <p>world</p>  
  15.         <input type="button" value="click" id="b1"/>  
  16.     </body>  
  17. </html>  

(2),子元素过滤选择器:返回所有匹配的父节点下的子节点
:nth-child(index/even/odd)
n从1开始

  1. <html>  
  2.     <head>  
  3.         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>  
  4.         <script>  
  5.             $(function(){  
  6.                 $('#b1').click(function(){  
  7.                     $('ul li:nth-child(1)').html('item100');  
  8.                 });  
  9.             });  
  10.         </script>  
  11.     </head>  
  12.     <body>  
  13.             <ul>  
  14.                 <li>item1</li>  
  15.                 <li>item2</li>  
  16.                 <li>item3</li>  
  17.   
  18.             </ul>  
  19.             <ul>  
  20.                 <li>item4</li>  
  21.                 <li>item5</li>  
  22.                 <li>item6</li>  
  23.   
  24.             </ul>  
  25.             <input type="button" value="click" id="b1"/>  
  26.     </body>  
  27. </html>  

(3),表单对象属性过滤选择器
:enabled
:disabled      //文本输入框不能输入
:checked//被选择的节点
:selected

5、表单选择器

:input       $(':input');返回所有的input
:text
:pasword
:checkbox
:radio
:submit
:image
:reset
:button

抱歉!评论已关闭.