Blogger Information
Blog 59
fans 6
comment 0
visits 52195
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
jQuery中常用DOM操作-js-43课8.19
希望
Original
727 people have browsed it

一、元素的添加,替换,删除

1、创建demo5.html,引入jquery-3.5.1.js

  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. <title>jQuery中常用DOM基础</title>
  7. <style>
  8. .active {
  9. color: red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <script src="../0818/lib/jquery-3.5.1.js"></script>
  15. <script>
  16. // 1. 元素的添加
  17. // 父元素.append(子元素)
  18. // var ol = $("<ol>");
  19. // $("body").append(ol);
  20. // $("body").append($("<ol>"));
  21. $("body").append("<ol>");
  22. // 子元素.appendTo(父元素)
  23. $("<li>").text("小芳").appendTo("ol");
  24. // 添加内容的时候,可又同步设置样式addClass
  25. $("<li>").addClass("active").text("小刘").appendTo("ol");
  26. // 在创建元素时,可以同步的生成属性
  27. $("<li>", {
  28. id: "hello",
  29. style: "background:lightblue",
  30. })
  31. .html('<a href="">小李</a>')
  32. .appendTo("ol");
  33. // append(callback)
  34. $("ol").append(function () {
  35. // 动态生成一定数量的<li>
  36. var lis = "";
  37. for (var i = 0; i < 5; i++) {
  38. lis += "<li>老学员 " + (i + 1) + "</li>";
  39. }
  40. return lis;
  41. });
  42. // 从第4个元素前面添加一个<li>, 原位置上. before(新元素)
  43. $("ol > li:nth-of-type(4)").before("<li>新学员1</li>");
  44. //新元素.insertAfter(原来的位置)
  45. $("<li>新学员2</li>").insertAfter("ol > li:nth-of-type(5)");
  46. // prepend(), prependTo(),最上面插入一个
  47. $('<li class="active">我是班长</li>').prependTo("ol");
  48. // 最后一个替换
  49. // 元素的替换: 要被替换掉的元素.replaceWith(新元素)
  50. $("ol > li:last-of-type").replaceWith("<h2>第十二期学员</h2>");
  51. // 删除元素 remove()
  52. $("li:nth-of-type(6)").remove();
  53. </script>
  54. </body>
  55. </html>

2、删除第6个元素后:

二、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. <title>getter / setter -2</title>
  7. <style>
  8. body {
  9. display: flex;
  10. flex-direction: column;
  11. align-items: center;
  12. color: #666;
  13. }
  14. form {
  15. width: 400px;
  16. /* height: 150px; */
  17. padding: 20px 10px;
  18. border: 1px solid #aaa;
  19. box-shadow: 0 0 5px #888;
  20. box-sizing: border-box;
  21. background-color: #eee;
  22. display: grid;
  23. grid-template-columns: 80px 200px;
  24. gap: 10px;
  25. place-content: center center;
  26. }
  27. button {
  28. grid-column: 2 / 3;
  29. height: 26px;
  30. }
  31. button:hover {
  32. color: white;
  33. background-color: #888;
  34. border: none;
  35. cursor: pointer;
  36. }
  37. .red {
  38. color: red;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <h2 class="red">用户登录</h2>
  44. <form action="handle.php" method="GET"">
  45. <label for="email">Email:</label>
  46. <input
  47. type="email"
  48. name="email"
  49. id="email"
  50. autofocus
  51. value="leture@php.cn"
  52. />
  53. <label for="password">Password:</label>
  54. <input type="password" name="password" id="password" value="不少于6位" />
  55. <label for="confirm">记住我:</label>
  56. <div>
  57. <input type="radio" name="save" id="confirm" value="1" checked /><label
  58. for="confirm"
  59. >保存</label
  60. >
  61. <input type="radio" name="save" id="cancel" value="0" /><label
  62. for="cancel"
  63. >放弃</label
  64. >
  65. </div>
  66. <button>登录</button>
  67. </form>
  68. <script src="../0818/lib/jquery-3.5.1.js"></script>
  69. <script>
  70. var form = $("form");
  71. // console.log(form);
  72. // css(): 设置元素的内联样式的
  73. // css(name): getter, 获取表单宽度
  74. console.log(form.css('width'));
  75. // 用原生的来获取非内联样式(计算样式)
  76. var form = document.forms.item(0);
  77. console.log(window.getComputedStyle(form,null).getPropertyValue("width")) ;
  78. // css(name, value): setter, 设置
  79. $('form').css('border', "3px dashed green")
  80. // css({...}): 支持同样设置多个样式属性
  81. $('form').css({
  82. 'border': "3px dashed blue",
  83. 'background': 'yellow'
  84. });
  85. // css(callback): 支持回调函数当参数,每次打开背景颜色不同
  86. $('form').css('background-color', function (){
  87. var bgcolors = ['plum', 'lightblue', 'tan', 'lime']; // index = [0, 3]
  88. // Math.floor()向下取整, 3.14 = 3, 4.8 => 4
  89. var randomIndex = Math.floor(Math.random()*bgcolors.length);
  90. return bgcolors[randomIndex];
  91. });
  92. </script>
  93. </body>
  94. </html>
  • 总结:
  • 元素添加,append(子元素)、appendTo(父元素)
  • 可在任何位置插入元素
  • 可替换任何位置的元素
  • css(): getter 获取表单宽度
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:OK
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