zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    iOS开发 日常错误积累
    Linux编程---I/O部分
    Binder机制1---Binder原理介绍
    hdu4405概率dp入门
    SSL连接建立过程分析(1)
    用GDB调试程序(一)
    RBAC权限管理
    HDU3930(离散对数与原根)
    Java实现 蓝桥杯VIP 算法提高 特殊的质数肋骨
    Java实现 蓝桥杯VIP 算法提高 产生数
  • 原文地址:https://www.cnblogs.com/i1991/p/7161387.html
Copyright © 2011-2022 走看看