javascript attributes属性是干什么的-LMLPHP

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

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 属性)。看看效果图:

javascript attributes属性是干什么的-LMLPHP

【推荐学习:javascript高级教程

以上就是javascript attributes属性是干什么的的详细内容,更多请关注Work网其它相关文章!

08-26 06:12