Blogger Information
Blog 34
fans 0
comment 0
visits 20046
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
DOM元素的增删改及留言板案例
OC的PHP大牛之路
Original
330 people have browsed it

DOM元素的增删改

  1. // 创建新元素:createElement()
  2. const ul=document.createElement('ul');
  3. // 添加元素:append()
  4. document.body.append(ul);
  5. for (let i=0;i<5;i++){
  6. const li=document.createElement('li');
  7. li.append('item-'+(i+1));
  8. ul.append(li);
  9. }


  1. // 在某节点之前插入:before
  2. const li=document.createElement('li');
  3. li.append('new item');
  4. li.style.color='blue';
  5. // 获取第二个元素
  6. const item=ul.querySelector('li:nth-of-type(2)');
  7. // 当前节点之前插入.before(新节点)
  8. item.before(li);
  9. // 克隆
  10. let newNode=li.cloneNode(true);
  11. // 当前节点之后插入.after(新节点)
  12. item.after(newNode);
  13. ul.style.border='2px solid yellow';
  14. const h3=document.createElement('h3');
  15. h3.append('商品列表')
  16. h3.style.color='red';
  17. // 在当前列表的前面插入一个标题:insertAdjacentElement('插入位置',元素)
  18. // beforebegin:开始标签之前
  19. ul.insertAdjacentElement('beforebegin',h3);
  20. // afterbegin:开始标签之后
  21. ul.insertAdjacentHTML('afterbegin','<li style="color:red">hello</li>');
  22. // beforeend:结束标签之前
  23. ul.insertAdjacentHTML('beforeend','<li style="color:red">world</li>');
  24. // afterend:结束标签之后
  25. ul.insertAdjacentHTML('afterend','<h4 style="color:red">共计:9个</h4>');


  1. // 替换:replaceChild()
  2. // 被替换的旧节点
  3. const targetNode=ul.querySelector('li:last-of-type');
  4. const link=document.createElement('a');
  5. // 新节点
  6. link.href='www.php.cn';
  7. link.append('php中文网');
  8. // parentNode.replaceChild(新节点,被替换的旧节点)
  9. ul.replaceChild(link,targetNode);
  10. // 移除:remove
  11. ul.firstElementChild.remove();
  12. ul.lastElementChild.remove();
  13. ul.querySelector('li:nth-of-type(2)').remove();
  14. ul.querySelector('li:nth-of-type(3)').remove();


留言板案例

  1. <body>
  2. <header>
  3. <h3>留言板</h3>
  4. </header>
  5. <main>
  6. <input type="text" onkeydown="addMsg(this)" placeholder="请输入留言" autofocus />
  7. <ul class="list"></ul>
  8. </main>
  9. <script>
  10. function addMsg(ele) {
  11. if (event.key === 'Enter') {
  12. if (ele.value.length === 0) {
  13. alert('留言不能为空');
  14. ele.focus();
  15. return false;
  16. }
  17. const ul = document.querySelector('.list');
  18. ele.value = ele.value + '<button onclick="del(this.parentNode)">删除</button>';
  19. ul.insertAdjacentHTML('afterbegin', `<li>${ele.value}</li>`);
  20. ele.value = null;
  21. }
  22. }
  23. function del(ele) {
  24. return confirm('是否删除?') ? ele.remove() : false;
  25. }
  26. </script>
  27. </body>
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