Blogger Information
Blog 31
fans 0
comment 0
visits 18230
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS第5课-jquery选择器表单属性-九期线上班
Content っ
Original
651 people have browsed it

1、常用jquery属性选择器案例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <script src="../1219/jquery-3.4.1.min.js"></script>
  6. <title>Title</title>
  7. </head>
  8. <body>
  9. <div class="mydiv" id="div1" style="background-color: #000000" >黑色</div>
  10. <div class="mydiv" id="div2" style="background-color: #ff0000" >红色</div>
  11. <div class="mydiv" id="div3" style="background-color: #00ff00" >
  12. <p id="a">div中的p标签</p>
  13. <div>
  14. <p id="b" p1="p1">div中的div的的p标签</p>
  15. <p id="bbw" p1="p1">div中的div的的p1=p1</p>
  16. <p id="bbq" p1="p2">div中的div的的p_bbq标签</p>
  17. <p id="c">p3</p>
  18. </div>
  19. <p id="d">pd4</p>
  20. <div style="background-color: #ffff00">最后一个div,也是div3中的第二个子div</div>
  21. </div>
  22. <p id="p4">我是p标签</p>
  23. <button onclick="jfirst()">:first</button>
  24. <button onclick="jeq()">:eq</button>
  25. <button onclick="jgt()">:gt</button>
  26. <button onclick="jlast()">:last</button>
  27. <button onclick="jlt()">:lt</button>
  28. <button onclick="jxz()">:[]</button>
  29. <button onclick="jxzq()">:[]_1</button>
  30. <button onclick="jbcz()">:不存在</button>
  31. <button onclick="jxzq1()">:^=</button>
  32. <button onclick="jxzq2()">:$=</button>
  33. <button onclick="jxzq3()">:*=</button>
  34. <button onclick="jfirstchild()">:first-child</button>
  35. <button onclick="jlastchild()">:last-child</button>
  36. <!--/***********js*************/-->
  37. <script>
  38. //选择第一个匹配的元素
  39. function jfirst() {
  40. var div1 = $('div:first');
  41. console.log(div1);
  42. }
  43. //匹配的集合中选择索引值为index的元素
  44. function jeq() {
  45. var div2 = $('div:eq(1)');
  46. console.log(div2);
  47. }
  48. //选择匹配集合中所有大于给定index(索引值)的元素
  49. function jgt() {
  50. var div = $('div:gt(1)');
  51. console.log(div);
  52. }
  53. //选择最后一个匹配的元素
  54. function jlast() {
  55. var div = $('div:last');
  56. console.log(div);
  57. }
  58. //选择匹配集合中所有小于于给定index(索引值)的元素
  59. function jlt() {
  60. var div = $('div:lt(1)');
  61. console.log(div);
  62. }
  63. //选择所有具有指定属性的元素,该属性可以是任何值。
  64. function jxz() {
  65. var p = $('p[p1]');
  66. console.log(p);
  67. var pid = $('p[id]');
  68. console.log(pid);
  69. }
  70. //选择指定属性值的元素
  71. function jxzq() {
  72. var p = $('p[p1=p1]');
  73. console.log(p);
  74. var pid = $('p[id=c]');
  75. console.log(pid);
  76. }
  77. //选择不存在指定属性,或者指定的属性值不等于给定值的元素
  78. function jbcz() {
  79. var p = $('p[p1!=p1]');
  80. console.log(p);
  81. }
  82. //选择指定属性是以给定字符串开始的元素
  83. function jxzq1() {
  84. var p = $('p[p1 ^= p1]');
  85. console.log(p);
  86. }
  87. function jxzq2() {
  88. var p = $('p[p1 $= p1]');
  89. console.log(p);
  90. }
  91. function jxzq3() {
  92. var p = $('p[p1 *= p1]');
  93. console.log(p);
  94. }
  95. //选择所有父级元素下的第一个子元素。
  96. function jfirstchild() {
  97. var p = $('p:first-child');
  98. console.log(p);
  99. }
  100. //选择所有父级元素下的最后一个子元素。
  101. function jlastchild() {
  102. var p = $('p:last-child');
  103. console.log(p);
  104. }
  105. </script>
  106. </body>
  107. </html>

2、常用jquery表单对象属性案例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>常用jquery表单对象属性案例</title>
  6. <script src="../1219/jquery-3.4.1.min.js"></script>
  7. </head>
  8. <body>
  9. <h2>常用jquery表单对象属性案例</h2>
  10. <form action="">
  11. <div>
  12. <label for="">用户名</label>
  13. <input type="text" name="username">
  14. </div>
  15. <div>
  16. <label for="">输入密码</label>
  17. <input type="password" name="password">
  18. </div>
  19. <div>
  20. <label for="">再次输入</label>
  21. <input type="password" name="repassword">
  22. </div>
  23. <div>
  24. <label for="">性别</label>
  25. <input type="radio" name="sex" value="1">
  26. <input type="radio" name="sex" value="0">
  27. </div>
  28. <div>
  29. <label for="">用户状态</label>
  30. <input type="checkbox" name="status" value="1">启用
  31. <input type="checkbox" name="status" value="0">禁用
  32. </div>
  33. <div>
  34. <label for="">籍贯</label>
  35. <select name="province" id="">
  36. <option value="beijing">北京</option>
  37. <option value="shanghai">上海</option>
  38. <option value="tianjin">天津</option>
  39. <option value="chongqing">重庆</option>
  40. </select>
  41. </div>
  42. <button type="button" onclick="save()">保存</button>
  43. </form>
  44. <script>
  45. function save() {
  46. var username = $("input[name = 'username']").val();
  47. var password = $('input[name = password]').val();
  48. var rePassword = $('input[name = repassword]').val();
  49. var sex = $('input[name = sex]:checked').val();
  50. var status = $('input[name = status]:checked').val();
  51. var province = $('select option:selected').text();
  52. if (username == ''){
  53. alert('请输入用户名');
  54. return;
  55. }
  56. if (password == ''){
  57. alert('请输入密码');
  58. return;
  59. }
  60. if (rePassword == ''){
  61. alert('请再次输入密码');
  62. return;
  63. }
  64. if (sex == ''){
  65. alert('请选择性别');
  66. return;
  67. }
  68. if (status == ''){
  69. alert('请选择状态');
  70. return;
  71. }
  72. if (province == ''){
  73. alert('请选择籍贯');
  74. return;
  75. }
  76. console.log(sex+'----'+status);
  77. var sexStr = parseInt(sex) == 1 ? '男' : '女';
  78. var statusStr = parseInt(status) == 1 ? '启用' : '禁用';
  79. alert('您的用户名:'+username+' 密码:'+password+' 二次密码:'+rePassword+' 性别:'+sexStr+' 状态:'+statusStr+' 籍贯:'+province);
  80. }
  81. </script>
  82. </body>
  83. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:表单验证很重要, 一定要重视
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