前端学习笔记-20-javascript中绑定事件的两种方式
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--等待页面加载完毕之后再执行代码-->
<script>
window.onload = function () {
let btnEle = document.getElementById('d1');
btnEle.onclick = function () {
alert('已经赐予资本家当头一棒')
}
}
</script>
</head>
<body>
<button onclick="func1()">点击打到资本家,打三棒</button>
<button id="d1">点击打到资本家,打一棒</button>
</body>
</html>
效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
function func1() {
alert("已经赐予资本家当头三棒")
}
</script>
</head>
<body>
<button onclick="func1()">点击打到资本家,打三棒</button>
</body>
</html>
效果
标签:function,打三棒,btnEle,绑定,20,Title,javascript,点击,资本家 来源: https://blog.csdn.net/CSDNXXCQ/article/details/121226354