1.原生JS綁定事件的過程:
element.addEventListener(event, function, useCapture);
兼容寫法
function addEventHandler(obj,type,fn){//添加事件監(jiān)聽
if(obj.addEventListener){
obj.addEventListener(type,fn,false);
}else{
obj.attachEvent('on'+type,fn);
};
}
2.JQuery中綁定事件的過程:
$('input').bind('click', function (e) { //接受事件對象參數(shù)
alert(e);
});
3、事件解綁
原生JS寫法:
兼容寫法
function removeEventHandler(obj,type,fn){
if(obj.removeEventListener){
obj.removeEventListener(type,fn);
}else{
obj.detachEvent("on"+type,fn);
}
}
JQuery中解綁事件寫法:
$('input').unbind('click');
遠近互聯(lián)前端小華整理發(fā)布,希望能對學(xué)習(xí)技術(shù)的你有所幫助
遠近互聯(lián)專業(yè)提供網(wǎng)站建設(shè)、APP開發(fā)、網(wǎng)站優(yōu)化、外貿(mào)網(wǎng)站SEO、微信運營的品牌整合營銷服務(wù)讓客戶通過網(wǎng)絡(luò)品牌建立與網(wǎng)絡(luò)傳播提高業(yè)績。






