Blogger Information
Blog 37
fans 1
comment 0
visits 27275
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1219_jquery选择器 第36课
叮叮当当
Original
773 people have browsed it

1 、ID选择器 $(‘#ID’),类选择器 $(‘.class’), 元素选择器 $(‘element’)

  1. <body>
  2. <input type="text" id="user" value="admin">
  3. <input type="text" class="phone" value="13912312345">
  4. <p id="paragraph">段落</p>
  5. <script type="text/javascript">
  6. //id选择器 $('#ID')
  7. var user = $('#user').val();
  8. //类选择器,$('.class'),class若有多个,取第一个
  9. var phone = $('.phone').val();
  10. //元素选择器 $('element')
  11. var paragraph = $('p').text();
  12. console.log( user );
  13. console.log( phone );
  14. console.log( paragraph );
  15. </script>
  16. </body>

2 、子孙代选择器,兄弟选择器

  1. <body>
  2. <div class="mydiv" style="background-color: #000;" id="div_black">黑色</div>
  3. <div class="mydiv" style="background-color: #ff0000;" id="div_red">红色</div>
  4. <p id="1">我是p标签</p>
  5. <div class="mydiv" style="background-color: #00ff00;" id="div_green">
  6. <p id="a">div中的p标签</p>
  7. <div>
  8. <p id="b">div中div中的p标签</p>
  9. <p id="c">P3</p>
  10. </div>
  11. <p id="d">div中的p标签</p>
  12. </div>
  13. <p id="2">我是p标签</p>
  14. <p id="3">我是p标签</p>
  15. <script type="text/javascript">
  16. // 与$('div p')效果一样,但可读性高
  17. var divp1 = $('#div_green p'); // 后代 (子孙代)
  18. console.log( divp1 );
  19. var divp2 = $('#div_green > p'); // 子代
  20. console.log( divp2 );
  21. var divp3 = $('#div_green ~ p'); // 后面所有兄弟元素,
  22. console.log( divp3 );
  23. var divp4 = $('#div_green + p'); // 后面第一个兄弟元素
  24. console.log( divp4 );
  25. </script>
  26. </body>

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这与原生的document.querySelectorAll()对应
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
Author's latest blog post