1
<p id="demo">单击按钮查看按钮元素有多少属性</p>
2
<button onclick="myFunction()">点我</button>
3
<script>
4
function myFunction() {
5
var a = document.getElementsByTagName("BUTTON")[0].attributes;
6
var x = document.getElementById("demo");
7
x.innerHTML = a.length;
8
}
9
</script>
10
<p>结果将是1 (按钮元素的onclick属性)。</p>
11
<p><strong>Note:</strong> In Internet Explorer 8 及更早的版本,attributes属性将返回所有元素的属性集合,并且在这个例子中,显示比1更大的数。</p>