Javascript 提供了getAttributeNode()方法来查找具有指定元素名称的属性节点,作为属性object。如果 属性 不存在,则返回值为null 或空字符串(“”)。
element.getAttributeNode(attributename);
它返回代表指定属性节点 的属性对象
在以下示例中,有两个带有不同class的标题标签。这些标签由getAttributeNode()访问时,可以返回它们所附加的属性类。它的作用与数组相同。我们只能通过提供它们的索引号来访问多个类。
<html>
<body>
<h2 class="class1">Tutorix</h2>
<h2 class="class2">Nhooo</h2>
<p id = "attribute"></p>
<script>
var elmnt = document.getElementsByTagName("h2")[1];
var value = elmnt.getAttributeNode("class").value;
document.getElementById("attribute").innerHTML = value;
</script>
</body>
</html>Tutorix Nhooo class2