Blogger Information
Blog 56
fans 1
comment 0
visits 62703
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JQ基本的DOM操作
零龙
Original
895 people have browsed it

jQuery基本的DOM操作

  • 在示例中使用了jQuery的Dom基本操作html。jQuery表单操作,jQuery操作CSS
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Jquery - Dom</title>
  8. </head>
  9. <body>
  10. <script src="js/jquery-3.5.1.js"></script>
  11. <script>
  12. $("body").append("<p>jQuery-DOM基础</p>");
  13. //jQuery 页面中添加<P>标签
  14. $("p").after("<hr>");
  15. //添加一个hr标签
  16. $("hr").after("<ul>");
  17. //添加UL
  18. var lis = "";
  19. //定义一个空值存放li
  20. $("ul").append(function(item){
  21. for(var i = 1; i < 5; i++)
  22. {
  23. lis += "<li>item"+ i +"</li>";
  24. }
  25. return lis;
  26. });
  27. //使用循环添加li标签
  28. $("ul li").eq(0).attr("id","test");
  29. //为UL中的第一个li添加一个id
  30. $("<li>我是用appendto添加的</li>").appendTo("ul");
  31. //在ul>li中添加新标签
  32. $("ul").prepend("<li>我是用prepend添加的</li>");
  33. $("<li>我是用prependTo添加的</li>").prependTo("ul");
  34. $("#test").css("color","blue");
  35. $("#test").html("我是被更改的text");
  36. $("ul li").eq(2).addClass("test");
  37. $("ul li").eq(2).css({
  38. "color":"white",
  39. "background":"green",
  40. }
  41. );
  42. $("ul").after("<hr>");
  43. $("hr").after("<span>");
  44. $("span").eq(1).html("jQuery-form");
  45. $("span").eq(1).after("<p>");
  46. $("p:nth-of-type(2)").after("<form>");
  47. $("form").attr("id","form");
  48. $("form").attr("class","form");
  49. $("form").attr("method","get");
  50. $("<label>").appendTo("form");
  51. $("label").html("用户:");
  52. $("<input>").appendTo("form");
  53. $("input").attr("id","user");
  54. $("input").attr("type","text");
  55. $("input").css({
  56. "width":"130px",
  57. "height": "25px",
  58. "font-size":"14px",
  59. "margin-bottom": "10px",
  60. });
  61. $("input").after("<br>");
  62. $("br").after("<label>");
  63. $("label:nth-of-type(2)").html("密码:");
  64. $("label:nth-of-type(2)").after("<input>");
  65. $("input").css({
  66. "width":"130px",
  67. "height": "25px",
  68. "font-size":"14px",
  69. "margin-bottom": "10px",
  70. });
  71. $("input").eq(1).attr("type","password");
  72. $("input").eq(1).after("<br>");
  73. $("br").eq(1).after("<label>");
  74. $("label:nth-of-type(3)").html("存储:");
  75. $("label:nth-of-type(3)").after("<input>");
  76. $("input").eq(2).attr("type","radio");
  77. $("input").eq(2).after("<span>");
  78. $("span").eq(2).html("保存");
  79. $("span").eq(2).after("<input>");
  80. $("input").eq(3).attr("type","radio");
  81. $("input").eq(3).attr("checked",true);
  82. $("input").eq(3).after("<span>");
  83. $("span").eq(3).html("不保存");
  84. $("span").eq(3).after("<br>");
  85. $("br").eq(2).after("<button>")
  86. $("button").html("提交");
  87. $("button").css({
  88. "background-color": "#4CAF50",
  89. "border": "1px solid green",
  90. "color": "white",
  91. "padding": "5px 24px",
  92. "cursor": "pointer",
  93. "float": "left",
  94. "margin-top": "10px",
  95. });
  96. $("button").after("<button>");
  97. $("button").eq(1).html("重置");
  98. $("button").eq(1).css({
  99. "background-color": "#4CAF50",
  100. "border": "1px solid green",
  101. "color": "white",
  102. "padding": "5px 24px",
  103. "cursor": "pointer",
  104. "float": "left",
  105. "margin-top": "10px",
  106. "margin-left":"10px",
  107. });
  108. $("button").eq(1).after("<br>");
  109. $("br").eq(3).after("<div>");
  110. $("div").attr("id","box");
  111. $("button").eq(0).click(function(ev) {
  112. ev.preventDefault();
  113. var user = $("#user").val();
  114. var tips = "";
  115. var users = ["admin","ynllww","dhhaha"];
  116. if(user.length === 0)
  117. {
  118. tips = "用户名不能为空";
  119. }else if(users.indexOf(user)=== -1 )
  120. {
  121. tips = '用户名不存在' + '<button type="button">请注册</button>';
  122. }else
  123. {
  124. tips = '<i style="color:green">用户名正确<i>';
  125. }
  126. if($("#user").next().get(0).tagName !== "SPAN"){
  127. $("#user").after("<span>");
  128. $('span').eq(2).html(tips).css('color','red').insertAfter(user);
  129. }
  130. });
  131. $("button").eq(1).click(function(ev){
  132. $("#user").val("");
  133. $("#password").val("");
  134. });
  135. $("input").eq(2).click(function()
  136. {
  137. $("input").eq(2).prop("checked",true);
  138. $("input").eq(3).prop("checked",false);
  139. // prop() 方法设置或返回被选元素的属性和值
  140. });
  141. $("input").eq(3).click(function()
  142. {
  143. $("input").eq(2).prop("checked",false);
  144. $("input").eq(3).prop("checked",true);
  145. });
  146. </script>
  147. </body>
  148. </html>

示例图

总结:对于基础的掌握,借助手册和度娘,勉强写完。还需要多写多看

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:并不是任何功能都要用jq, 原生方便就用原生, 别太在意这个
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