Blogger Information
Blog 62
fans 3
comment 1
visits 29693
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js演示dom基本操作
kiraseo_wwwkiraercom
Original
357 people have browsed it

实例演示dom增删改的常用方法

代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title> 实例演示dom增删改的常用方法</title>
  8. </head>
  9. <body>
  10. <ul class="list">
  11. <li class="item">item</li>
  12. <li class="item">item</li>
  13. <li class="item">item</li>
  14. <li class="item">item</li>
  15. <li class="item">item</li>
  16. <li class="item">item</li>
  17. </ul>
  18. <ul class="xz"></ul>
  19. <ul class="sc">
  20. <li class="item">删除1</li>
  21. <li class="item">删除2</li>
  22. <li class="item">删除3</li>
  23. <li class="item">删除4</li>
  24. <li class="item">删除5</li>
  25. <li class="item">删除6</li>
  26. </ul>
  27. <ul class="xg">
  28. <li class="item">修改1</li>
  29. <li class="item">修改2</li>
  30. <li class="item">修改3</li>
  31. <li class="item">修改4</li>
  32. <li class="item">修改5</li>
  33. <li class="item">修改6</li>
  34. </ul>
  35. <script>
  36. const xz= document.querySelector(".xz");
  37. console.log(xz);
  38. const li ='<li>新增</li>'
  39. xz.insertAdjacentHTML('afterbegin',li);
  40. const ul = document.querySelector('.sc');
  41. // 删除
  42. ul.firstElementChild.remove();
  43. ul.querySelector('li:nth-of-type(3)').remove();
  44. ul.querySelector('li:nth-of-type(4)').remove();
  45. //修改
  46. const xg = document.querySelector('.xg');
  47. console.log(xg);
  48. const bxgNode = xg.querySelector("li:nth-of-type(3)");
  49. console.log(bxgNode);
  50. const nr = document.createElement('span')
  51. nr.append("我被修改了!!");
  52. xg.replaceChild(nr, bxgNode);
  53. </script>
  54. </body>
  55. </html>

效果图

留言板案例, 添加自定义样式

代码如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>留言板案例, 添加自定义样式</title>
  8. </head>
  9. <body>
  10. <input type="text" onkeydown="addMsg(this)" placeholder="请输入留言" autofocus />
  11. <ul class="list"></ul>
  12. <script>
  13. function addMsg(ele){
  14. if (event.key === 'Enter') {
  15. // 1. 判断留言不能为空
  16. if (ele.value.length === 0) {
  17. alert('留言不能为空');
  18. // 重置焦点到输入框中
  19. ele.focus();
  20. return false;
  21. }
  22. // 2. 添加留言
  23. const ul = document.querySelector('.list');
  24. ele.value = ele.value + ' <a onclick="del(this.parentNode)">删除x</a>';
  25. ul.insertAdjacentHTML('afterbegin', `<li>${ele.value}</li>`);
  26. // 查询找到当前a标签
  27. const a = document.querySelector(".list a");
  28. // 添加a标签鼠标样式
  29. a.style.cursor ="pointer";
  30. //增加a标签字体颜色
  31. a.style.color = 'red';
  32. // 增加a标签1px的边框
  33. a.style.border= '1px solid';
  34. // 3. 清空输入框
  35. ele.value = null;
  36. }
  37. }
  38. // 删除功能
  39. function del(ele) {
  40. if (confirm('是否删除?')) {
  41. ele.remove();
  42. }
  43. }
  44. </script>
  45. </body>
  46. </html>

效果图

Correcting teacher:PHPzPHPz

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