javascript 操作节点的属性

使用层次关系访问节点

parentNode:返回节点的父节点

childNodes:返回子节点集合,childNodes[i]

firstChild:返回节点的第一个子节点,最普遍的用法是访问该元素的文本节点

firstElementChild:返回节点的第一个元素节点

lastChild:返回节点的最后一个子节点

lastElementChild:返回节点的最后一个子节点的元素节点

nextSibling:下一个节点

previousSibling:上一个节点

setAttribute()设置某个节点的属性值

  节点对象.setAttribute("属性名","属性值")

getAttribute()获取某个节点的属性值

  节点对象.getAttribute("属性名")

例如:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>node节点</title>
</head>
<script>
  onload=function(){
    var node1=document.getElementsByTagName("ul")[0];
    //console.log(node1.childNodes);
    node1.setAttribute("xxyy","1122")
    console.log(node1.firstElementChild.getAttribute("xxyy"));
  }
</script>

<body>
  <ul>
    <li id="a" xxyy="ooxx">asdf</li>
    <li id="b"></li>
    <li id="c"></li>
  </ul>
</body>
</html>

原文地址:https://www.cnblogs.com/i1991/p/7161387.html