定义和用法
attributes 属性返回指定节点属性的集合。
提示: 你可以使用 length 属性确定属性的数量,然后你可以遍历所有的属性节点提取你想要的信息。
提示: 每个属性都是可用属性节点对象。
浏览器支持:
所有主要浏览器都支持 attributes 属性
注意: 在 Internet Explorer 8 以及更早的版本中,attributes 属性会返回元素所有可能属性的集合。
语法
node.attributes技术细节
| 返回值: | NamedNodeMap 对象, 表示属性的集合。 |
|---|---|
| DOM 版本 | Core Level 1 Node Object |
实例
获取元素属性的集合:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<p id="demo">单击按钮,查看按钮元素有多少个属性:</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var btn=document.getElementsByTagName("BUTTON")[0];
var x=document.getElementById("demo");
x.innerHTML=btn.attributes.length;
}
</script>
<p>结果应为1(按钮元件的onclick属性)</p>
<p><strong>注意:</strong> 在Internet Explorer 8 或者更早版本中,attributes 属性将返回所有可能元素的集合 ,并将在这个示例中显示一个比1更高的数字。</p>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4327.html
支付宝
微信