javascript attributes属性是干什么的

来自:互联网
时间:2021-10-25
阅读:

attributes 属性返回指定节点的属性集合,即 NamedNodeMap对象。

提示:您可以使用 length 属性来确定属性的数量,然后您就能够遍历所有的属性节点并提取您需要的信息。

语法:node.attributes

返回值:NamedNodeMap 对象,表示属性的集合。

<!DOCTYPE html>
<html>
<body>

<p>点击按钮来查看 button 元素拥有多少属性。</p>

<button id="myBtn" onclick="myFunction()">试一下</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myBtn").attributes.length;
    document.getElementById("demo").innerHTML = x;
}
</script>


</body>
</html>

我们猜猜结果是多少?结果应该是 2(button 元素的 id 和 onclick 属性)。看看效果图:

<a href=https://www.freexyz.cn/dev/JavaScript/ target=_blank class=infotextkey>JavaScript</a> attributes属性是干什么的

返回顶部
顶部