定义和用法
length 属性可返回集合中节点的选项数目。
浏览器兼容:
所有主要浏览器都支持 length 属性
注意: Internet Explorer 8 及更早 IE 版本,length属性将返回一个元素所有可能的属性数目。
语法
namednodemap.length技术细节
| 返回值: | 数字, 代表节点的数目。 |
|---|---|
| DOM 版本 | Core Level 1 |
实例
返回按钮元素的属性数目:
<!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 a=document.getElementsByTagName("BUTTON")[0].attributes;
var x=document.getElementById("demo");
x.innerHTML=a.length;
}
</script>
<p>结果将是1 (按钮元素的onclick属性)。</p>
<p><strong>Note:</strong> In Internet Explorer 8 及更早的版本,attributes属性将返回所有元素的属性集合,并且在这个例子中,显示比1更大的数。</p>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4299.html
支付宝
微信