web前端面试第一次[addEventListenr();绑定事件]

//当一个元素同时处理多个函数,这里使用按钮

//addEventListener(string类型,处理函数,boolean);

<input type="button" id="btn"/>
//同一个按钮btn
document.getElementById("btn").addEventListener(
      //(事件因为是string类型,所有要用双引号括起来),匿名函数,(boolean值为true时,在捕获阶段触发事件,false时,在冒泡阶段触发事件)
"click",function(){
          console.log("函数1");
},false
)
//同一个按钮btn
document.getElementById("btn").addEventListener(
"click",function(){
          console.log("函数2");
},false
)
//同一个按钮btn
document.getElementById("btn").addEventListener(
"click",function(){
          console.log("函数3");
},false
)

//removeEventListener 解绑事件方法
//解绑事件必须使用命名函数

猜你喜欢

转载自www.cnblogs.com/MOMING95/p/10778032.html