Blogger Information
Blog 47
fans 0
comment 3
visits 45078
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Dom元素操作-增删改查
江流
Original
550 people have browsed it
  • html

    1. <ul class="list">
    2. <li class="item">首页</li>
    3. <li class="item">学习</li>
    4. <li class="item">相册</li>
    5. <li class="item">查看</li>
    6. <li class="item">社区</li>
    7. <li class="item">关于</li>
    8. </ul>
  • css

    1. .list > .item {
    2. list-style-type: none;
    3. padding: 1rem;
    4. }
    5. .itemStart {
    6. background-color: #ddd;
    7. }
    8. .active {
    9. background-color: lightseagreen;
    10. color: lightyellow;
    11. }
  • javascript

    1. // 获取body对象
    2. const body = document.body;
    3. // 设置body对象的fontSize
    4. body.style.fontSize = "12px";
    5. // 通过类名,获取列表对象
    6. const list = document.querySelector(".list");
    7. list.style.display = "flex";
    8. // 通过类名,获取列表内人项
    9. const items = document.querySelectorAll(".list>.item");
    10. // 为列表项添加类
    11. items.forEach((item) => item.classList.add("itemStart"));
    12. // 获取列表里的第一项
    13. const first = document.querySelector(".list>.item");
    14. first.classList.add("active");
    15. // 元素节点,children方式获取的对象集合,不是数组,
    16. // 通过"...",把获取的对象集合变成数组
    17. [...list.children].forEach((item) => {
    18. item.style.border = "1px solid #b3b3b3";
    19. });
    20. list.firstElementChild.nextElementSibling.style.color = "blue";
    21. list.lastElementChild.style.backgroundColor = "#aaa";
    22. list.lastElementChild.previousElementSibling.style.color = "seagreen";
    23. // 创建元素,并添加元素到列表
    24. let li = document.createElement("li");
    25. li.textContent = "联系我们";
    26. li.style.padding = "1rem";
    27. li.style.backgroundColor = "#ddd";
    28. li.style.listStyleType = "none";
    29. // 添加方法append
    30. list.append(li);
    31. // 查询到"学习"项
    32. const study = document.querySelector(".list>.item:nth-of-type(2)");
    33. // 删除“学习”项
    34. study.remove();

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