jQuery实践——选择器篇

时间:2023-11-18 13:17:08

       一、基本

    1. #id:
      html:<div id="demo1">demo1</div>
      
      jQuery:$("#demo1").css("background","red");

      jQuery实践——选择器篇

    2. element:
      html:<div>demo2</div>
      
      jQuery:$("div").css("background","blue");

      jQuery实践——选择器篇

    3. .class:
      html:<div class="demo3">demo3</div>
      
      jQuery:$(".demo3").css("background","yellow");

      jQuery实践——选择器篇

    4. *
      jQuery:$("*").css("background","green");

      jQuery实践——选择器篇

    5. selector1,selector2,selectorN
      html:<div class="demo3">demo3</div>
      <div id="demo4">demo4</div> jQuery:$(".demo3,#demo4").css("background","orange");

      jQuery实践——选择器篇

      二.层级:

    6. ancestor descendant
      html:<div><span>demo5</span></div>
      
      jQuery:$("div span").css("background","aqua");

      jQuery实践——选择器篇

    7. parent > child
      html:<div><p>demo6</p></div>
      
      jQuery:$("div>p").css("background","purple");

      jQuery实践——选择器篇

    8. prev + next
      html:<div><p>demo6</p></div>
      <p>demo7</p> iQuery:$("div+p").css("background","chartreuse");

      jQuery实践——选择器篇

    9. prev ~ siblings
      html:<p>demo7</p>
      <div>demo8</div>
      <div>demo8</div> jQuery:$("p~div").css("background","skyblue");

      jQuery实践——选择器篇

      3.基本筛选器:

    10. :first
      html:        <ul>
      <li>demo9</li>
      <li>demo9</li>
      <li>demo9</li>
      <li>demo9</li>
      <li>demo9</li>
      </ul> Jquery:$("li:first").css("background","crimson");

      jQuery实践——选择器篇

    11. :not

      jQuery:$("li:not(:first)").css("background","cadetblue");

      jQuery实践——选择器篇

    12. :even

      jQuery:$("li:even").css("background","fuchsia");

      jQuery实践——选择器篇

    13. :odd

      jQuery:$("li:odd").css("background","greenyellow");

      jQuery实践——选择器篇

    14. :eq(index)

      jQuery:$("li:eq(2)").css("background","gold");

      jQuery实践——选择器篇

    15. :gt(index)

      jQuery:$("li:gt(2)").css("background","blueviolet");

      jQuery实践——选择器篇

    16. :lang

      html:       <ul>
      <li>demo9</li>
      <li>demo9</li>
      <li lang="en">demo9</li>
      <li>demo9</li>
      <li>demo9</li>
      </ul> jQuery: $("li:lang(en)").css("background","salmon");

      jQuery实践——选择器篇

    17. :last:

      jQuery:$("li:last").css("background","slateblue");

      jQuery实践——选择器篇

    18. :lt(index)
      jQuery:$("li:lt(2)").css("background","teal");

      jQuery实践——选择器篇

    19. :header
      html:<h1>demo10</h1>
      <h2>demo10</h2> jQuery:$(":header").css("background","darkred");

      jQuery实践——选择器篇

    20. :animated:
      html:<div id="demo11">demo11</div>
      
      jQuery:$("#demo11").click(function(){
              $("#demo11:not(:animated)").animate({
                width:"+=20px"
              },1000);
           });

      jQuery实践——选择器篇

    21. :focus
      html:<input type="text" id="demo12" value="demo12" autofocus="autofocus"/>
      
      jQuery:$("input:focus").css("background","darkkhaki");

      jQuery实践——选择器篇

    22. :root
      jQuery:$(":root").css("background","seagreen");

      jQuery实践——选择器篇

    23. :target
      不会使用

      三、内容

    24. :contains(text)
      html:<div>demo13</div>
      
      jQuery:$("div:contains('demo13')").css("background","turquoise");

      jQuery实践——选择器篇

    25. :empty
      html:<input type="text" id="demo14" placeholder="demo14" />
      
      jQuery:$("input:empty").css("background","gray");

      jQuery实践——选择器篇

    26. :has(selector)
      html:<div><p>demo15</p></div>
      
      jQuery:$("div:has(p)").css("background","darkgreen");

      jQuery实践——选择器篇

    27. :parent
      html:<div><p>demo16</p></div>
      
      jQuery:$("p:parent").css("background","hotpink");

      jQuery实践——选择器篇

      四、可见性

    28. :hidden
      html:<div id="demo17" style="display: none;"></div>
      
      jQuery:console.log($("div:hidden"));

      jQuery实践——选择器篇

    29. :visible
      html:<p>demo18</p>
      
      jQuery:$("p:visible").css("background","lightseagreen");

      jQuery实践——选择器篇

      五、属性

    30. [attribute] 
      html:  <p id="demo18">demo19</p>
      <p id="demo19">demo19</p>
      <p id="test19">demo19</p>
      <p>demo19</p>
      jQuery:$("p[id]").css("background","olive");

      jQuery实践——选择器篇

    31. [attribute=value] 
      jQuery:$("p[id='demo18']").css("background","lightcoral");

      jQuery实践——选择器篇

    32. [attribute!=value] 
      jQuery:$("p[id!='demo18']").css("background","darkslategray");

      jQuery实践——选择器篇

    33. [attribute^=value] 
      jQuery:$("p[id^='demo']").css("border","1px solid red");

      jQuery实践——选择器篇

    34. [attribute$=value] 
      jQuery:$("p[id$='19']").css("border","1px solid blue");

      jQuery实践——选择器篇

    35. [attribute*=value] 
      jQuery:$("p[id*='1']").css("border","1px solid green");

      jQuery实践——选择器篇

    36. [attrSel1][attrSel2][attrSelN]
      html:<div id="demo20" class="demo20">demo20</div>
      
      jQuery:$("div[id][class = 'demo20']").css("border","1px solid black");

      jQuery实践——选择器篇

      六、子元素

      html:  <ul>
      <li>demo21</li>
      <li>demo21</li>
      <li>demo21</li>
      <li>demo21</li>
      <li>demo21</li>
      </ul>
    37. :first-child 
      $("ul li:first-child").css("border","1px solid blueviolet")

      jQuery实践——选择器篇

    38. :first-of-type
      $("li:first-of-type").css("border","1px solid green");

      jQuery实践——选择器篇

    39. :last-child 
      $("ul li:last-child").css("border","1px solid lightcoral");

      jQuery实践——选择器篇

    40. :last-of-type
      $("ul li:last-of-type").css("border","1px solid turquoise");

      jQuery实践——选择器篇

    41. :nth-child() 
      $("ul li:nth-child(2)").css("border","1px solid firebrick");

      jQuery实践——选择器篇

    42. :nth-last-child()
      $("ul li:nth-last-child(2)").css("border","1px solid blueviolet");

      jQuery实践——选择器篇

    43. :nth-last-of-type()
      $("ul li:nth-last-of-type(2)").css("border","1px solid fuchsia");

      jQuery实践——选择器篇

    44. :nth-of-type()
      $("ul li:nth-of-type(2)").css("border","1px solid crimson");

      jQuery实践——选择器篇

    45. :only-child 
      $("ul li:only-child").css("border","1px solid teal");

      jQuery实践——选择器篇

    46. :only-of-type
      $("ul li:only-of-type").css("border","1px solid orange");

      jQuery实践——选择器篇

      七、表单

      html:<form >
      <input type="text" value="demo22" disabled="disabled"/><br />
      <input type="password" /><br />
      <input type="radio" name="radio" checked="checked"/><label>radio1</label>
      <input type="radio" name="radio"/><label>radio2</label><br />
      <input type="checkbox" /><label>checkbox1</label>
      <input type="checkbox" checked="checked"/><label>checkbox2</label>
      <input type="checkbox" /><label>checkbox3</label><br />
      <input type="file"disabled="disabled"/><br />
      <input type="hidden" />
      <select name="selector">
      <option value="1">selector1</option>
      <option value="2">selector2</option>
      <option value="3" selected="selected">selector3</option>
      </select><br />
      <input type="submit" value="提交" /><br />
      <input type="image" value="images" /><br />
      <input type="reset" value="重置"/><br />
      <input type="button" value="btn1"/>
      <button>btn2</button>
      </form>
    47. :input 
      $(":input").css("border","1px solid darkblue");

      jQuery实践——选择器篇

    48. :text 
      $(":text").css("color","goldenrod");

      jQuery实践——选择器篇

    49. :password
      $(":password").css("background","fuchsia");

      jQuery实践——选择器篇

    50. :radio
    51. :checkbox
      console.log($(":radio"));
      console.log($(":checkbox"));

      jQuery实践——选择器篇

    52. :submit 
      $(":submit").css("border","1px solid red");

      jQuery实践——选择器篇

    53. :image
      $(":image").css("border","1px solid blue");

      jQuery实践——选择器篇

    54. :reset
      $(":reset").css("background","green");

      jQuery实践——选择器篇

    55. :button
      $(":button").css("background","blue");

      jQuery实践——选择器篇

    56. :file 
      $(":file").css("border","1px solid orange");

      jQuery实践——选择器篇

    57. :hidden 
      console.log($("input:hidden"));

      jQuery实践——选择器篇

      九、表单属性

    58. :enabled
      $("input:enabled").css("color","red");

      jQuery实践——选择器篇

    59. :disabled
      $("input:disabled").css("color","blue");

      jQuery实践——选择器篇

    60. :checked 
      console.log($("input:checked"));
    61. :selected 
      console.log($("select option:selected").html());

      jQuery实践——选择器篇