<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>句柄添加监听事件</title>
<script type="text/javascript" src="tzy.js"></script>
</head>
<body>
<button id="mybtn" onclick="demo()">按钮</button>
<script>
/*
//注意这种引用样式必须放在button后面不然出现
//Uncaught TypeError: Cannot read property 'addEventListener' of null
//错误:页面未加载完成
var x = document.getElementById("mybtn");
x.addEventListener("click", hello);
x.addEventListener("click", world);//添加
x.removeEventListener("click", world);//移除
function hello() {
alert("hello");
}
function world() {
alert("world");
}*/
</script>
</body>
</html>
//注意这种引用样式必须放在button后面不然出现
//Uncaught TypeError: Cannot read property 'addEventListener' of null
//错误:页面未加载完成
function demo(){
var x = document.getElementById("mybtn");
x.addEventListener("click", hello);
x.addEventListener("click", world);//添加
x.removeEventListener("click", world);//移除
}
function hello() {
alert("hello");
}
function world() {
alert("world");
}