代码编辑器:
x
1
<p id="demo">点击按钮查看按钮元件具有一个onclick属性</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.hasAttribute("onclick");
8
};
9
</script>
10
<p>IE 8 及更早版本不支持 hasAttribute 方法</p>
11