DOM之Attr类型

461 阅读1分钟

Attr类型

介绍

元素的属性在DOM中以Attr类型来表示。

Attr类型特点:

特点
nodeType 2
nodeName 属性的名称
nodeValue 属性的值
parentNode null
子节点 没有(不支持)

Attr对象的属性

Attr对象包含三个属性:namevaluespecified

name: 与nodeName的值相同。

value: 与nodeValue的值相同。

specified: 是一个布尔值,用以区别特 性是在代码中指定的,还是默认的。

<html>
  <body>
    <div id="myDiv" title="this div"></div>
  </body>
  <script>
    var div = document.getElementById('myDiv');
    console.log(div.getAttributeNode('title').name); // "title"
    console.log(div.getAttributeNode('title').value); // "this div"
    console.log(div.getAttributeNode('title').specified); // true
  </script>
</html>

不建议直接访问属性节点。实际上使用getAttribute()setAttribute()removeAttribute()方法更为方便。


- RZeeY