歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用jQuery基本過濾器選擇器選擇元素

使用jQuery基本過濾器選擇器選擇元素

日期:2017/3/1 10:58:20   编辑:Linux編程

示例代碼如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>使用jQuery基本過濾選擇器</title>
  5. <script language="javascript" type="text/javascript"
  6. src="../Jscript/jquery-1.5.2.js"></script>
  7. <style type="text/css">
  8. body{font-size:12px;text-align:center}
  9. div{width:241px;height:83px;border:solid 1px #eee}
  10. h1{font-size:13px}
  11. ul{list-style-type:none;padding:0px}
  12. .DefClass,.NotClass{height:23px;width:60px;line-height:23px;float:left;
  13. border-top:solid 1px #eee;border-bottom:solid 1px #eee}
  14. .GetFocus{width:58px;border:solid 1px #666;background-color:#eee}
  15. #spnMove{width:238px;height:23px;line-height:23px;}
  16. </style>
  17. <script type="text/javascript">
  18. $(function(){ //增加第一個元素的類別,獲取第一個元素 返回值單個元素
  19. $("li:first").addClass("GetFocus");
  20. })
  21. $(function(){ //增加最後一個元素的類別,獲取最後一個元素 返回值單個元素
  22. $("li:last").addClass("GetFocus");
  23. })
  24. $(function(){ //增加去除所有與給定選擇器匹配的元素類別 返回值元素集合
  25. $("li:not(.NotClass)").addClass("GetFocus");
  26. })
  27. $(function(){ //增加所有索引值為偶數的元素類別,從0開始計數 返回值元素集合
  28. $("li:even").addClass("GetFocus");
  29. })
  30. $(function(){ //增加所有索引值為奇數的元素類別,從0開始計數 返回值元素集合
  31. $("li:odd").addClass("GetFocus");
  32. })
  33. $(function(){ //增加一個給定索引值的元素類別,從0開始計數 返回值單個元素
  34. $("li:eq(1)").addClass("GetFocus");
  35. })
  36. $(function(){ //增加所有大於給定索引值的元素類別,從0開始計數 返回值元素集合
  37. $("li:gt(1)").addClass("GetFocus");
  38. })
  39. $(function(){ //增加所有小於給定索引值的元素類別,從0開始計數 返回值元素集合
  40. $("li:lt(4)").addClass("GetFocus");
  41. })
  42. $(function(){ //增加標題類元素類別
  43. $("div h1").css("width","238");
  44. $(":header").addClass("GetFocus");
  45. })
  46. $(function(){
  47. animateIt(); //增加動畫效果元素類別
  48. $("#spnMove:animated").addClass("GetFocus");
  49. })
  50. function animateIt() { //動畫效果
  51. $("#spnMove").slideToggle("slow", animateIt);
  52. }
  53. </script>
  54. </head>
  55. <body>
  56. <div>
  57. <h1>基本過濾選擇器</h1>
  58. <ul>
  59. <li class="DefClass">Item 0</li>
  60. <li class="DefClass">Item 1</li>
  61. <li class="NotClass">Item 2</li>
  62. <li class="DefClass">Item 3</li>
  63. </ul>
  64. <span id="spnMove">Span Move</span>
  65. </div>
  66. </body>
  67. </html>
說明:選擇器animated在捕捉動畫效果元素時,先自定義一個動畫效果函數animateIt(),然後執行該函數,選擇器才能獲取動畫效果,並增加其類別。
Copyright © Linux教程網 All Rights Reserved