事件绑定、事件监听、事件委托

在JavaScript中有三种常用的绑定事件的方法

  • 在DOM元素中直接绑定;
  • 在JavaScript代码中绑定;
  • 绑定事件监听函数。

在DOM元素中直接绑定

我们可以在DOM元素上绑定onclick、onmouseover、onmouseout、onmousedown、onmouseup、ondblclick、onkeydown、onkeypress、onkeyup等。好多不一一列出了。如果想知道更多事件类型请查看

1
2
3
4
5
6
<input type="button" value="click me" onclick="hello()">
<script>
function hello(){
alert("hello world!");
}
</script>

在JavaScript代码中绑定

1
2
3
4
5
6
<input type="button" value="按钮" id="btn">
<script>
document.getElementById("btn").onclick = function(){
alert("hello world!");
}
</script>

绑定事件监听函数

绑定事件的另一种方法是用 addEventListener() 或 attachEvent() 来绑定事件监听函数

事件监听

关于事件监听,W3C规范中定义了3个事件阶段,依次是捕获阶段、目标阶段、冒泡阶段。

1
element.addEventListener(event, function, useCapture)
  • event : (必需)事件名,支持所有DOM事件。
  • function: (必需)指定要事件触发时执行的函数。
  • useCapture:(可选)指定事件是否在捕获或冒泡阶段执行。(true,捕获。),(false,冒泡。默认false。)
    1
    2
    3
    4
    5
    6
    7
    <input type="button" value="click me" id="btn1">
    <script>
    document.getElementById("btn1").addEventListener("click",hello);
    function hello(){
    alert("hello world!");
    }
    </script>

注:IE8以下不支持

IE标准

语法:

1
element.attachEvent(event, function)

  • event: (必需)事件类型。需加“on“,例如:onclick。
  • function:(必需)指定要事件触发时执行的函数。
    1
    2
    3
    4
    5
    6
    7
    <input type="button" value="click me" id="btn2">
    <script>
    document.getElementById("btn2").attachEvent("onclick",hello);
    function hello(){
    alert("hello world!");
    }
    </script>

事件监听的优点

1. 可以绑定多个事件
1
2
3
4
5
6
7
8
9
10
11
// 常规的事件绑定只执行最后绑定的事件。
<input type="button" value="click me" id="btn3">
<script>
var btn3 = document.getElementById("btn3");
btn3.onclick = function(){
alert("hello 1"); //不执行
}
btn3.onclick = function(){
alert("hello 2"); //执行
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
//两个事件都执行了。
<input type="button" value="click me" id="btn4">
<script>
var btn4 = document.getElementById("btn4");
btn4.addEventListener("click",hello1);
btn4.addEventListener("click",hello2);
function hello1(){
alert("hello 1");
}
function hello2(){
alert("hello 2");
}
</script>
2. 可以解除相应的绑定
1
2
3
4
5
6
7
8
9
10
11
12
13
<input type="button" value="click me" id="btn5">
<script>
var btn5 = document.getElementById("btn5");
btn5.addEventListener("click",hello1);//执行了
btn5.addEventListener("click",hello2);//不执行
btn5.removeEventListener("click",hello2);
function hello1(){
alert("hello 1");
}
function hello2(){
alert("hello 2");
}
</script>

封装事件监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<input type="button" value="click me" id="btn5">
//绑定监听事件
function addEventHandler(target,type,fn){
if(target.addEventListener){
target.addEventListener(type,fn);
}else{
target.attachEvent("on"+type,fn);
}
}
//移除监听事件
function removeEventHandler(target,type,fn){
if(target.removeEventListener){
target.removeEventListener(type,fn);
}else{
target.detachEvent("on"+type,fn);
}
}
//测试
var btn5 = document.getElementById("btn5");
addEventHandler(btn5,"click",hello1);//添加事件hello1
addEventHandler(btn5,"click",hello2);//添加事件hello2
removeEventHandler(btn5,"click",hello1);//移除事件hello1