代码编辑器:
x
1
<p id="demo">单击按钮获取按钮元素中onclick属性的值</p>
2
<button onclick="myFunction()">点我</button>
3
<script>
4
function myFunction(){
5
var a=document.getElementsByTagName("BUTTON")[0];
6
var x=document.getElementById("demo");
7
x.innerHTML=a.attributes.getNamedItem("onclick").textContent;
8
}
9
</script>
10
<p><strong>注意:</strong> Internet Explorer 8 及更早之前的版本不支持getNamedItem 方法</p>
11