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