dom对象(DOM对象、document对象的常用方法、var和let区别、节点、查找结点、 查看/修改/删除属性节点、创建和增加节点)

一、查看节点

  • getElementById( ) 元素的ID名称来访问,返回对拥有指定id的第一个对象的引用
  • getElementsByName( )  按元素的name名称来访问,返回带有指定名称的对象的集合
  • getElementsByTagName( )按标签来访问,返回带有指定标签名的对象的集合

   
   
  1. <body>
  2. <div id="box">hello box </div>
  3. </body>
  4. <script>
  5. var oDiv = document. getElementById( "box"); //div元素对象
  6. // var oDiv = document.getElementsByTagName("div")[0];
  7. // console.log(oDiv.nodeName); //节点名称 DIV
  8. // console.log(oDiv.tagName); //标签名称 DIV
  9. // console.log(oDiv.nodeType); //1 节点类型
  10. // console.log(oDiv.nodeValue); //null 节点值
  11. // var attr = oDiv.getAttributeNode("id");
  12. // console.log(attr);
  13. // alert(attr); //[object Attr]属性对象
  14. // console.log(attr.nodeName); //节点名称 属性名
  15. // console.log(attr.nodeType); //节点类型 2
  16. // console.log(attr.nodeValue); //节点值 属性值
  17. var ch = oDiv. firstChild; //第一个子节点
  18. // console.log(ch);
  19. // alert(ch); //[object Text] 文本对象节点
  20. console. log(ch. nodeName); //节点名称 文本
  21. console. log(ch. nodeType); //节点类型 3
  22. console. log(ch. nodeValue); //节点值 文本值
  23. // get获取
  24. // Attribute 属性
  25. // Node 节点
  26. // 节点类型:
  27. // 元素节点 1
  28. // 属性节点 2
  29. // 文本节点 3
  30. </script>

二、查看节点

1、根据层次关系查找节点

(1) 通过父节点对象查找子节点对象

  •      父节点对象.firstChild      获取当前元素的第一个子节点
  •      父节点对象.lastChild      获取当前元素的最后一个子节点
  •      父节点对象.childNodes  获取当前元素的所有子节点 可能会有空格
  •      父节点对象.firstElementChild 查找父节点下的第一个子元素节点
  •      父节点对象.lastElementChild 查找父节点下的最后一个子元素节点
  •      父节点对象.children:获取当前元素的所有子元素

 (2)通过子节点对象查找父节点对象

  •      子节点对象.parentElement    通过子节点查找父元素
  •      子节点对象.parentNode         获取当前元素的父元素

(3)通过子节点查找兄弟节点

  •       子节点对象.previousSibling   获取当前元素的前一个兄弟节点
  •       子节点对象.nextSibling           获取当前元素的后一个兄弟节点
  •       子节点对象.previousElementSibling   获取当前元素的前一个兄弟节点
  •       子节点对象.nextElementSibling           获取当前元素的后一个兄弟节点

   
   
  1. <body>
  2. <ul id="box">
  3. <li id="list">列表1 </li>
  4. <li>列表2 </li>
  5. <li>列表3 </li>
  6. </ul>
  7. </body>
  8. <script>
  9. var oUl = document. getElementById( "box");
  10. var oLi = document. getElementById( "list");
  11. // console.log(oUl.firstChild);//第一个子节点 可能有文本节点 换行
  12. // console.log(oUl.firstElementChild);//第一个子元素节点
  13. // console.log(oUl.lastChild);//最后一个子节点 可能有文本节点 换行
  14. // console.log(oUl.lastElementChild);//最后一个子元素节点
  15. // console.log(oUl.childNodes);//ul里的所有的子节点对象 可能有文本节点
  16. console. log(oUl. children); //ul里的所有的子元素节点对象
  17. // console.log(oUl.firstChild.nodeType); //3
  18. // console.log(oUl.firstChild.nodeValue); //
  19. // console.log(oUl.childNodes.length); //7
  20. // var elArr = [];
  21. // for (var i = 0; i < oUl.childNodes.length; i++) {
  22. // // console.log(oUl.childNodes[i]);
  23. // if(oUl.childNodes[i].nodeType === 1){
  24. // elArr.push(oUl.childNodes[i])
  25. // }
  26. // }
  27. // console.log(elArr);
  28. console. log(oLi. parentNode); //查找父节点
  29. console. log(oLi. parentElement); //查找父元素
  30. // 父节点查找子节点
  31. // 通过父节点对象查找子节点对象(可能会有文本节点) 兼容性好
  32. // 父节点对象.firstChild 查找父节点下的第一个子节点
  33. // 父节点对象.lastChild 查找父节点下的最后一个子节点
  34. // 父节点对象.childNodes 多个 查找父节点下的所有的子节点
  35. // 通过父节点对象查找子元素节点对象
  36. // 父节点对象.firstElementChild 查找父节点下的第一个子元素节点
  37. // 父节点对象.lastElementChild 查找父节点下的最后一个子元素节点
  38. // 父节点对象.children 多个 查找父节点下的所有的子元素节点
  39. // 子节点查找父节点
  40. // 子节点对象.parentNode 通过子节点查找父节点
  41. // 子节点对象.parentElement 通过子节点查找父元素
  42. </script>

三、查看/修改/删除属性节点

  • 查看属性节点:getAttribute("属性名")
  • 修改属性节点:setAttribute("属性名","属性值")
  • 删除属性节点:removeAttribute(“属性名”)

   
   
  1. <body>
  2. <ul id="box" a="10">
  3. <li>列表1 </li>
  4. <li id="list">列表2 </li>
  5. <li>列表3 </li>
  6. </ul>
  7. </body>
  8. <script>
  9. var oUl = document. getElementById( "box");
  10. // console.log(oUl.id);
  11. // console.log(oUl.a);//无法获取自定义的属性值
  12. // console.log(oUl.getAttribute("id"));
  13. // console.log(oUl.getAttribute("a")); //可以获取自定义属性
  14. // 设置
  15. // oUl.title = "hello"
  16. // oUl.b = 100;
  17. oUl. setAttribute( "title", "hello")
  18. oUl. setAttribute( "b", 100);
  19. // oUl.removeAttribute("a");
  20. oUl. removeAttribute( "a");
  21. // 节点对象.getAttribute("属性名") 根据属性名获取属性值
  22. // 节点对象.setAttribute("属性名","属性值")
  23. // 节点对象.removeAttribute("属性名") 删除对应的这对属性
  24. </script>

四、创建和增加节点 的方法

  • createElement():创建元素节点  
  • appendChild():末尾追加方式插入节点
  • insertBefore():在指定节点前插入新节点
  • cloneNode():克隆节点

   
   
  1. <style>
  2. img{
  3. width: 100px;
  4. }
  5. </style>
  6. </head>
  7. <body>
  8. <div id="box">
  9. hello world
  10. </div>
  11. <button id="btn">点击添加图片 </button>
  12. </body>
  13. <script>
  14. var oDiv = document. getElementById( "box");
  15. var oBtn = document. getElementById( "btn");
  16. // var oSpan = document.createElement("span");
  17. oBtn. onclick = function( ){
  18. var oImg = document. createElement( "img"); //创建图片节点对象
  19. // oImg.src = ""
  20. oImg. setAttribute( "src", "./img/biaoqing.webp"); //设置图片节点对象src属性
  21. // oDiv.appendChild(oImg);//把创建好的图片节点对象追加到div内部最后
  22. document. body. appendChild(oImg); //把创建好的图片节点对象追加到body内部最后
  23. }
  24. // console.log(oSpan);//元素对象
  25. // console.log(oImg);//元素对象
  26. // alert(oSpan);
  27. // oSpan.innerHTML = "我是span";
  28. // console.log(oSpan);//元素对象
  29. // 追加
  30. // oDiv.appendChild(oSpan);
  31. // 创建元素对象
  32. // document.createElement("元素名称")
  33. // 追加元素对象到父元素内部最后
  34. // 父节点对象.appendChild(子节点)
  35. </script>

五、属性操作 


   
   
  1. <body>
  2. <ul id="box" a="10">
  3. <li>列表1 </li>
  4. <li id="list">列表2 </li>
  5. <li>列表3 </li>
  6. </ul>
  7. </body>
  8. <script>
  9. var oUl = document. getElementById( "box");
  10. // console.log(oUl.id);
  11. // console.log(oUl.a);//无法获取自定义的属性值
  12. // console.log(oUl.getAttribute("id"));
  13. // console.log(oUl.getAttribute("a")); //可以获取自定义属性
  14. // 设置
  15. // oUl.title = "hello"
  16. // oUl.b = 100;
  17. oUl. setAttribute( "title", "hello")
  18. oUl. setAttribute( "b", 100);
  19. // oUl.removeAttribute("a");
  20. oUl. removeAttribute( "a");
  21. // 节点对象.getAttribute("属性名") 根据属性名获取属性值
  22. // 节点对象.setAttribute("属性名","属性值")
  23. // 节点对象.removeAttribute("属性名") 删除对应的这对属性
  24. </script>



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值