Blogger Information
Blog 36
fans 2
comment 0
visits 23609
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月20号:jquery 选择器2
Rambo-Yang
Original
775 people have browsed it

1,:first:选择第一个匹配的DOM元素。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3</div>
  4. <br><br>
  5. <button class="btn" onclick="jfirst()">:first</button>
  6. <script>
  7. function jfirst() {
  8. var obj = $('div:first');
  9. console.log(obj);
  10. }
  11. //结果:选中<div id="div_black" style=" background: #000;color: #fff">div1</div>
  12. </script>

2,:eq(index):在匹配的集合中选择索引值为index的元素。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3</div>
  4. <br><br>
  5. <button onclick="jeq()">:eq(index)</button>
  6. <script>
  7. function jeq() {
  8. var obj = $('div:eq(0)');
  9. console.log(obj);
  10. }
  11. //结果:选中<div id="div_black" style=" background: #000;color: #fff">div1</div>
  12. </script>

3,:gt(index):选择匹配集合中所有大于给定index(索引值)的元素,index: 从0开始计数的索引值。可以为负数,-index: 从0开始计数的索引值。 从最后一个元素开始反向计数。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. </div>
  5. <p>P1</p>
  6. <br><br>
  7. <button onclick="jgt()">:gt(index)</button>
  8. <script>
  9. function jgt() {
  10. var obj = $('div:gt(0)');
  11. console.log(obj);
  12. }
  13. //选中<div id="div_red" style="background: #ff0000">div2</div><div id="div_green" style="background: green">div3</div>
  14. </script>

4,:lt(index):选择匹配集合中所有索引值小于给定index参数的元素。index: 从 0 开始计数的索引值。-index: 从0开始计数的索引值。 从最后一个元素开始反向计数。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. </div>
  5. <p>P1</p>
  6. <br><br>
  7. <button onclick="jlt()">:lt(index)</button>
  8. <script>
  9. function jlt() {
  10. var obj = $('div:lt(1)');
  11. console.log(obj);
  12. }
  13. //选中<div id="div_black" style=" background: #000;color: #fff">div1</div>
  14. </script>

5,:last:选择最后一个匹配的元素。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. <div id="div_s1">div3_1</div>
  5. </div>
  6. <p>P1</p>
  7. <br><br>
  8. <button onclick="jlast()">:last</button>
  9. <script>
  10. function jlast() {
  11. var obj = $('div:last');
  12. console.log(obj);
  13. }
  14. //选中<div id="div_s1">div3_1</div>
  15. </script>

6,[attribute]:attribute: 一个属性名 ,选择所有具有指定属性的元素,该属性可以是任何值。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div class="myclass" id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. <div id="div_s1">div3_1</div>
  5. </div>
  6. <p>P1</p>
  7. <br><br>
  8. <button onclick="jattr()">attribute</button>
  9. <script>
  10. function jattr() {
  11. var obj = $('div[class]');
  12. console.log(obj);
  13. }
  14. //选中<div class="myclass" id="div_red" style="background: #ff0000">div2</div>
  15. </script>

7,[attribute=value]:attribute: 一个属性名.value: 一个属性值。选择指定属性是给定值的元素。

  • [attribute!='value']: (不等于)选择不存在指定属性
  • [attribute^='value']:(指定字符开头)选择指定属性是以给定字符串开始的元素
  • [attribute$='value']:(指定字符结尾)选择指定属性是以给定值结尾的元素
  • [attribute*='value']:(模糊匹配)选择给定的属性是以包含某些值的元素
  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div class="myclass" id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. <div id="div_s1">div3_1</div>
  5. </div>
  6. <p>P1</p>
  7. <br><br>
  8. <button onclick="jattr()">attribute</button>
  9. <script>
  10. function jattr() {
  11. var obj = $('div[class="myclass"]');
  12. console.log(obj);
  13. }
  14. //选中<div class="myclass" id="div_red" style="background: #ff0000">div2</div>
  15. </script>

8,:first-child:选择所有父级元素下的第一个子元素。

  1. <div id="div_black" style=" background: #000;color: #fff">div1</div>
  2. <div class="myclass" id="div_red" style="background: #ff0000">div2</div>
  3. <div id="div_green" style="background: green">div3
  4. <div id="div_s1">div3_1</div>
  5. <p class="p2">p2</p>
  6. <p class="p3">p3</p>
  7. </div>
  8. <p>P1</p>
  9. <br><br>
  10. <button onclick="first_child()">first-child</button>
  11. <script>
  12. function first_child() {
  13. var obj = $('div div:first-child');
  14. console.log(obj);
  15. }
  16. //选中的是<div id="div_s1">div3_1</div>
  17. //如果obj = $('div p:first-child');这样定义 则选取不到任何元素,因为p标签不是第一个
  18. </script>

9,:checked:匹配所有勾选的元素。适用于复选框 (checkbox) ,单选框(radio button)

10,:selected:匹配所有选中的option元素

  1. <form>
  2. <div>
  3. <label>用户名</label>
  4. <input type="text" name="username">
  5. </div>
  6. <div>
  7. <label>密码</label>
  8. <input type="password" name="pwd">
  9. </div>
  10. <div>
  11. <label>再输一次</label>
  12. <input type="password" name="repwd">
  13. </div>
  14. <div>
  15. <label>性别</label>
  16. <input type="radio" name="sex" value="1">
  17. <input type="radio" name="sex" value="2">
  18. </div>
  19. <div>
  20. <label>籍贯</label>
  21. <select name="province">
  22. <option value="anhui">安徽</option>
  23. <option value="zhejiang">浙江</option>
  24. <option value="henan">河南</option>
  25. <option value="fujian" selected>福建</option>
  26. </select>
  27. </div>
  28. <div>
  29. <label>用户状态</label>
  30. <input type="checkbox" name="status" value="0">禁用
  31. </div>
  32. <button type="button" onclick="save()">保存</button>
  33. </form>
  34. <script>
  35. function save() {
  36. var username = $('input[name="username"]').val();
  37. var pwd = $('input[name="pwd"]').val();
  38. var repwd = $('input[name="repwd"]').val();
  39. var sex = $('input[name="sex"]:checked').val();
  40. var select = $('select option:selected').text();
  41. var status = $('input[name="status"]:checked').val();
  42. if (username == ''){
  43. alert('请填写用户名');
  44. return;
  45. }
  46. if (pwd != repwd || pwd ==''){
  47. alert('两次密码不一致!');
  48. return;
  49. }
  50. if (sex == undefined){
  51. alert('请选择性别!');
  52. return;
  53. }
  54. if(status == undefined) {
  55. alert('请选择用户状态');
  56. return;
  57. }
  58. alert('用户名:' + username + ' 密码:' + pwd + ' 性别:'+ sex + ' 籍贯:' + select + ' 用户状态:' + status)
  59. }
  60. </script>

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
Author's latest blog post