Maison > interface Web > js tutoriel > le corps du texte

JavaScript中绑定事件的三种方式及去除绑定

高洛峰
Libérer: 2016-12-07 15:30:47
original
1431 Les gens l'ont consulté

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

第一种办法

函数写在结构层里面

非常不好,使页面很混乱,行为与结构得不到分离

<input type="button" onclick="func();">
Copier après la connexion

绑定事件的第二种办法

好处:行为与结构开始分离

缺点:

第二种绑定方式中只能给一个时间绑定一个处理函数

即.onclick = fn1; . onclick = fn2 最终的效果是onclick = fn2

<select name="xueli" >
 <option value="">请选择学历</option>
 <option value="大学" >大学</option>
 <option value="中学">中学</option>
 <option value="初中">初中</option>
 <option value="小学">小学</option>
</select>
<form action="">
 <p>Email:<input type="text" name="email">
  姓名:<input type="text" name="ming" >
 </p>
</form>
Copier après la connexion

document.getElementsByTagName(&#39;select&#39;)[0].onclick= function (){
  alert(&#39;嘻嘻&#39;);
 }
document.getElementsByName(&#39;email&#39;)[0].onblur=function (){
 alert(&#39;哈哈哈&#39;);
}
Copier après la connexion

window.onload = function(){
 var d = document.getElementById(&#39;school&#39;);
 function fn1(){
  alert(&#39;hello&#39;);
 }
 function fn2(){
  alert(&#39;world&#39;);
 }
 d.onclick = fn1;//赋值操作 最终显示fn2
 d.onclick = fn2;
}
Copier après la connexion

绑定事件的第三种办法

//错误写法1
window.onload = function(){
 var d = document.getElementById(&#39;school&#39;);
 function fn1(){//this此时指向window
  this.style.background = &#39;blue&#39;;
 }
 function fn2(){//this此时指向window
  this.style.background = &#39;red&#39;;
 }
 //写一个匿名函数
 //最终的出现错误
 d.onclick = function (){
  fn1();
  fn2();
  //fn1 fn2是属性window的 实际上是这样 window.fn1() window.fn2()
   
   
 }
}
Copier après la connexion

下面这种写法没有问题 但是给DOM树额外增加了两个变量

window.onload = function(){
 var d = document.getElementById(&#39;school&#39;);
 d.fn1 = function (){//fn1是d的属性 最终this此时指向DOM对象
  this.style.background = &#39;blue&#39;;
 }
 d.fn2 = function (){//this此时指向DOM对象
  this.style.background = &#39;red&#39;;
 }
  
 //匿名函数 调用上面两个函数
 d.onclick = function (){
  this.fn1();
  this.fn2();
 }
}
Copier après la connexion

不在使用onclick

window.onload = function(){
 var d = document.getElementById(&#39;school&#39;);
 //达到了一次绑定两个函数
 d.addEventListener(&#39;click&#39;,function () {alert(&#39;blue&#39;);this.style.background =&#39;blue&#39;});
 d.addEventListener(&#39;click&#39;,function () {alert(&#39;red&#39;);this.style.background =&#39;red&#39;});
  
}
Copier après la connexion

去除绑定 不能用匿名函数 匿名函数 当时产生 当时消失

var fn1 = function () {alert(&#39;blue&#39;);this.style.background =&#39;blue&#39;};
var fn2 = function () {alert(&#39;red&#39;);this.style.background =&#39;red&#39;};
  
function adde(){
  var d = document.getElementById(&#39;school&#39;);
  d.addEventListener(&#39;click&#39;,fn1);
  d.addEventListener(&#39;click&#39;,fn2);
 }
function reme(){
 var d = document.getElementById(&#39;school&#39;);
 //d.removeEventListener(&#39;click&#39;,fn1);//只剩fn1
 d.removeEventListener(&#39;click&#39;,fn2);
}
Copier après la connexion

在IE下第三种绑定事件的方法

<div id="school">
  
 </div>
 <input type="button" value="加事件" onclick="adde();">
 <input type="button" value="减事件" onclick="reme();">
Copier après la connexion

var fn1 = function () {alert(&#39;blue&#39;);this.style.background =&#39;blue&#39;};
var fn2 = function () {alert(&#39;red&#39;);this.style.background =&#39;red&#39;};
  
function adde(){
  var d = document.getElementById(&#39;school&#39;);
  // IE6,7是后绑定的事件先发生
  d.attachEvent(&#39;onclick&#39;,fn1);
  d.attachEvent(&#39;onclick&#39;,fn2); //fn2先发生
 }
function reme(){
 var d = document.getElementById(&#39;school&#39;);
 //d.deltachEvent(&#39;click&#39;,fn1);//只剩fn1
 d.deltachEvent(&#39;click&#39;,fn2);
}
Copier après la connexion

   


Étiquettes associées:
js
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!