<html>
<head>
</head>
<body>
<button id="a1">点击</button>
<script>
function test(){
document.getElementById("a1").onclick=function(){alert('this is a1');};
//下面这种写法是不对的
//document.getElementById("a1").onclick="alert('this is a1');";
}
test();
</script>
</body>
</html>
本文介绍了一种使用JavaScript为HTML按钮动态绑定点击事件的方法,并对比了直接在HTML元素中定义事件处理程序的方式,强调了使用JavaScript进行事件绑定的优势。
1391

被折叠的 条评论
为什么被折叠?



