+ -
当前位置:首页 → 问答吧 → Jquery学习笔记(三)

Jquery学习笔记(三)

时间:2009-10-17

来源:互联网

·Jquery过滤选择器:

1、基本过滤选择器
此项选择器搭配基本选择器可以讲占到实际选择器应用的90%以上(可由document.getElementById及节点nodeChilds得知)

:first//(选取第一个元素)
:last//(选取最后一个元素)
:even//(选取索引是偶数的所有元素)
:odd//(选取索引是奇数的所有元素)
:eq(index)//(选取索引等于index的元素)
:gt(index)//(选取索引大于index的元素)
:lt(index)//(选取索引小于index的元素)
:header//(选取所有的h1,h2,h3等标题元素)
:animated//(选取当前正在执行动画的所有元素)

2、内容过滤选择器

:contains(text)//选取含有文本内容为text的元素
:empty//选取不包含子元素或者文本的空元素
:has(selector)//选取含有选择器所有匹配的元素的元素
:parent//选取含有子元素或者文本的元素

3、可见性过滤选择器
对于<input type="hidden" />不要考虑对其应用任何css属性。That’s no way。

:hidden//选取所有不可见的元素(包括<input type="hidden" />、<div style="display:none">和<div style="visibility:hidden;">;若只选取<input type="hidden" />使用$("input:hidden")
:visible//选取所有可见元素

4、属性选择过滤器
在ie6中对css的属性过滤选择器不起作用,而Jquery的属性选择过滤器经测试后在ie6中有效。(6.0之前不起作用)想要了解^ $ *可参考相关正则表达式资料。

[attribute]//选取拥有此属性的元素
[attribute=value]//选取属性的值为value的元素
[attribute!=value]//选取属性的值不为value的元素
[attribute^=value]//选取属性的值以value开始的元素
[attribute$=value]//选取属性的值以value结束的元素
[attribute*=value]//选取属性的含有value的元素
[selector1][selector2][selectorN]//属性选择器合并成一个复合属性选择器,注意此处为属性选择器的并集,如$("div[id][class$='Bar']"

5、子元素选择过滤器
同上面的属性选择器一样,原先的css规则对ie6不起作用,经过jquery可以选定该类元素。

:nth-child(index/even/odd/equation)//选取第index个子元素或者奇偶元素
:first-child//选取每个父元素的第一个元素(返回整个文档中每个元素的第一个子元素),如$("ul li:first-child");选择每个<ul>中第1个元素
:last-child//选取每个父元素的最后一个元素
:only-child//若某子元素是其父元素中惟一的子元素,将会被匹配

6、表单对象属性过滤选择器

:enabled//选择所有可用元素,例$("#form1:enabled")
:disabled//选取所有不可用元素
:checked//选取所有被选中元素(checkbox,radio)
:selected//选取所有被选中元素(下拉列表)

7、表单选择器
不再赘述

:input
:text
:password
:radio
:checkbox
:submit
:image
:reset
:button
:file
:hidden

以下实例综合讲解了几个选择器的使用,同时演示了使用jquery实现隔行换色、荧光棒特效、复选框checkbox全选反选效果
复制代码
  1.     <script type="text/javascript">
  2.     $(function(){
  3.         $("table tr:even").addClass("tdOdd");
  4.         $("th:first").css("background","#B4C6C1");//首个
  5.         $("table tr").mouseover(function(){
  6.             $(this).addClass("tdOver");}).mouseout(function(){
  7.             $(this).removeClass("tdOver");}).click(function(){//荧光棒
  8.             $(".tdClick").removeClass("tdClick");$(this).addClass("tdClick");
  9.         })//行锁定
  10.         $("input:checkbox:first").click(function(){
  11.             $("input:checkbox:not(input:checkbox:first)").each(function(){//剔除本身
  12.                 $(this).attr("checked",$("input:checkbox:first").attr("checked"));
  13.             })
  14.         })
  15.         $("input:checkbox:not(input:checkbox:first)").click(function(){
  16.             var flag=true;
  17.             $("input:checkbox:not(input:checkbox:first)").each(function(){
  18.                 if(!this.checked){flag=false;}//不可使用if($(this).attr("checked","false")){flag=false;}
  19.             });
  20.             $("input:checkbox:first").attr("checked",flag);
  21.         })
  22.     });
  23.     </script>
  24.     <style type="text/css">
  25.     body{
  26.     font-size:12px;
  27.     color:#366;
  28.     }
  29.     table{
  30.     border:none;
  31.     background:#fefefe;
  32.     width:100%;
  33.     border-collapse:collapse;
  34.     }
  35.     th{
  36.     background:#CFDEC6;
  37.     padding:4px;
  38.     color:#000;
  39.     }
  40.     td,.tdNormal{
  41.     border:#cfdec6 solid 1px;
  42.     padding:4px;
  43.     background:fefefe;
  44.     }
  45.     .tdOdd{
  46.     background:#f1fefa;
  47.     }
  48.     .tdOver{
  49.     background:#F5FAF7;
  50.     }
  51.     </style>
  52.     </head>
  53.     <body>
  54.     <table>
  55.         <tr>
  56.             <th>姓名</th>
  57.             <th>年龄</th>
  58.             <th>专业</th>
  59.         </tr>
  60.         <tr>
  61.             <td>王洪剑</td>
  62.             <td>22</td>
  63.             <td>电气自动化</td>
  64.         </tr>
  65.         <tr>
  66.             <td>李川川</td>
  67.             <td>20</td>
  68.             <td>计算机</td>
  69.         </tr>
  70.         <tr>
  71.             <td>陈超</td>
  72.             <td>22</td>
  73.             <td>计算机</td>
  74.         </tr>
  75.         <tr>
  76.             <td>秦玉龙</td>
  77.             <td>21</td>
  78.             <td>计算机</td>
  79.         </tr>
  80.         <tr>
  81.             <td>刘威</td>
  82.             <td>21</td>
  83.             <td>计算机</td>
  84.         </tr>
  85.         <tr>
  86.             <td>张会会</td>
  87.             <td>21</td>
  88.             <td>计算机</td>
  89.         </tr>
  90.         <tr>
  91.             <td>胡海生</td>
  92.             <td>30</td>
  93.             <td>计算机</td>
  94.         </tr>
  95.         <tr>
  96.             <td>吴雄</td>
  97.             <td>22</td>
  98.             <td>计算机</td>
  99.         </tr>  
  100.     </table>


预览效果

http://www.51obj.cn/

作者: walkingp   发布时间: 2009-10-17

热门下载

更多