下面的代码中,HTML定义了五个按钮,JS为五个按钮绑了click事件,然而我没通过document.getElementById获取每一个button元素,为什么这样事件也能绑定成功?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test_btn</title>
<style type="text/css">
button {padding: 5px 10px;}
</style>
</head>
<body>
<button id="btn">alert btn</button>
<button id="btn1">alert btn1</button>
<button id="btn2">alert btn2</button>
<button id="btn3">alert btn3</button>
<button id="btn4">alert btn4</button>
</body>
<script type="text/javascript">
btn.onclick = function () {
alert("btn");
}
btn1.onclick = function () {
alert("btn1");
}
btn2.onclick = function () {
alert("btn2");
}
btn3.onclick = function () {
alert("btn3");
}
btn4.onclick = function () {
alert("btn4");
}
</script>
</html>
当年话下
相关分类