Blogger Information
Blog 38
fans 1
comment 0
visits 28862
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月19日_jQuery选择器(基本选择器和层级选择器)
fkkf467
Original
926 people have browsed it

一、基本选择器

1. #id

匹配元素中的id属性的值

  1. <body>
  2. <div id="div1">div1</div>
  3. <p id="p1">p1</p>
  4. <div id="div2">div2</div>
  5. <p id="p2">p2</p>
  6. <script type="text/javascript">
  7. console.log($('#div1'));
  8. console.log($('#p2'));
  9. </script>
  10. </body>

2. .class

根据给定的css类名匹配元素

  1. <body>
  2. <div class="myClass">div1</div>
  3. <div class="myClass">div2</div>
  4. <p class="myClass">p1</p>
  5. <div class="mydiv">div3</div>
  6. <p class="myp2">p2</p>
  7. <script type="text/javascript">
  8. console.log($('.myClass'));
  9. console.log($('.myp2'));
  10. </script>
  11. </body>

3. element

根据给定的元素标签名匹配所有元素

  1. <body>
  2. <div id="div1">div1</div>
  3. <div id="div2">div2</div>
  4. <p id="p1">p1</p>
  5. <p id="p2">p2</p>
  6. <p id="p3">p3</p>
  7. <script type="text/javascript">
  8. var div = $('div');
  9. console.log(div);
  10. var p = $('p');
  11. console.log(p);
  12. </script>
  13. </body>

二、层级选择器

1. ancestor descendant

匹配给定元素的所有后代元素

  1. <body>
  2. <form>
  3. <label for="uaername">用户名</label>
  4. <input type="text" id="uaername" name="username">
  5. <label for="pwd">密码</label>
  6. <input type="password" id="pwd" name="pwd">
  7. <div id="d1">
  8. <label for="email">邮箱</label>
  9. <input type="email" id="email" name="email">
  10. </div>
  11. </form>
  12. <input type="text" name="none" id="one">
  13. <script type="text/javascript">
  14. var temp = $('form input');
  15. console.log(temp);
  16. console.log($('#d1 *')); // id为d1的所有子元素
  17. console.log($('#d1 input'));
  18. </script>
  19. </body>

2. parent > child

在给定的父元素下匹配所有的直接子元素

  1. <body>
  2. <form>
  3. <label for="uaername">用户名</label>
  4. <input type="text" id="uaername" name="username">
  5. <label for="pwd">密码</label>
  6. <input type="password" id="pwd" name="pwd">
  7. <div id="d1">
  8. <label for="email">邮箱</label>
  9. <input type="email" id="email" name="email">
  10. <p>
  11. <input type="number" id="number">
  12. </p>
  13. </div>
  14. </form>
  15. <input type="text" name="none" id="one">
  16. <script type="text/javascript">
  17. console.log($('form > input'));
  18. console.log($('#d1 > input'));
  19. </script>
  20. </body>

3. prev + next

匹配所有紧接在 prev 元素后的 next 元素

  1. <body>
  2. <div id="div1">div1</div>
  3. <div id="div2">
  4. <div id="div2_1">div2_1</div>
  5. <p>
  6. <div id="div_p">div_p</div>
  7. </p>
  8. </div>
  9. <p id="pw">pw</p>
  10. <div id="div3">div3</div>
  11. <script type="text/javascript">
  12. console.log($('#div1 + *'));
  13. console.log($('#div1 + div'));
  14. console.log($('#div1 + p')); // 找不到
  15. console.log($('p + div'));
  16. </script>
  17. </body>

4. prev ~ siblings

匹配 prev 元素之后的所有 siblings 元素

  1. <body>
  2. <div id="div1">div1</div>
  3. <div id="div2">
  4. <div id="div2_1">div2_1</div>
  5. <p>
  6. <div id="div_p">div_p</div>
  7. </p>
  8. </div>
  9. <p id="pw">pw</p>
  10. <div id="div3">div3</div>
  11. <script type="text/javascript">
  12. console.log($('#div1 ~ div'));
  13. console.log($('#div1 ~ p'));
  14. console.log($('#div2 ~ *'));
  15. </script>
  16. </body>

三、总结

基本掌握了jQuery基本选择器和层级选择器的用法。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:选择器, 有意思, 与css有相似之处, 但区别也很明显
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments