博主信息
博文 48
粉丝 0
评论 3
访问量 58088
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
Dom元素操作-增删改查
江流
原创
699人浏览过
  • 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();

批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学