自己的js工具 Event封裝
更新時(shí)間:2009年08月21日 01:46:18 作者:
說到瀏覽器中的event,相信不少人都很頭疼,ie的event大部分時(shí)候都可以獲取到
因?yàn)閕e的event是全局的而firefox的event是局部的,用起來不太方便,這個(gè)時(shí)候我們就要自己組裝一下常用的event操作了,封裝成類便于重用
/**
類 Event
用法:
Event.getEvent();獲取 ie,firefox的event
Event.getTarget();獲取ie的srcElement或firefox的target
Event.isIe();是否為ie
Event.clientX(); 獲取ie,fox的鼠標(biāo)x坐標(biāo)
Event.clientY();獲取 ie,fox的鼠標(biāo)y坐標(biāo)
*/
var Event=new function(){
this.toString=function(){
return this.getEvent();
}
//獲取 事件
this.getEvent=function(){
var ev=window.event;
if(!ev){
var c=this.getEvent.caller;
while(c){
ev=c.arguments[0];
if(ev && Event ==ev.constructor)
break;
c=c.caller;
}
}
return ev;
};
//獲取 事件源
this.getTarget=function(){
var ev=this.getEvent();
return this.isIe()?ev.srcElement:ev.target;
}
//是否為ie
this.isIe=function(){
return document.all?true:false;
}
//鼠標(biāo)x坐標(biāo)
this.clientX=function(){
var ev=this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//鼠標(biāo)y坐標(biāo)
this.clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**增加事件(對(duì)象,事件類型,函數(shù)指針 )
obj: html對(duì)象
sEvent: 事件名稱
spNotify: 事件執(zhí)行的方法
isCapture:是否允許全屏捕捉
*/
this.addEvent=function(obj,sEvent,fpNotify,isCapture){
sEvent=sEvent.indexOf("on")!=-1?sEvent:"on"+sEvent;
if(obj.addEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2);
obj.addEventListener(sEvent,fpNotify,isCapture);
}else{ //ie
if(isCapture)
obj.setCapture(isCapture);
obj.attachEvent(sEvent,fpNotify);
}
}
//移除事件
this.removeEvent=function(obj,sEvent,fpNotify){
if(obj.removeEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2)
obj.removeEventListener(sEvent,fpNotify,false);
}else{
obj.detachEvent(sEvent,fpNotify);
}
}
//獲取鼠標(biāo)按鍵,left=1,middle=2,right=3
this.button=function(){
var ev=this.getEvent();
if(!ev.which&&ev.button){//ie
return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return ev.which;
};
//阻止事件冒泡傳遞
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
else
ev.stopPropagation();
}
//阻止默認(rèn)事件返回
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}
復(fù)制代碼 代碼如下:
/**
類 Event
用法:
Event.getEvent();獲取 ie,firefox的event
Event.getTarget();獲取ie的srcElement或firefox的target
Event.isIe();是否為ie
Event.clientX(); 獲取ie,fox的鼠標(biāo)x坐標(biāo)
Event.clientY();獲取 ie,fox的鼠標(biāo)y坐標(biāo)
*/
var Event=new function(){
this.toString=function(){
return this.getEvent();
}
//獲取 事件
this.getEvent=function(){
var ev=window.event;
if(!ev){
var c=this.getEvent.caller;
while(c){
ev=c.arguments[0];
if(ev && Event ==ev.constructor)
break;
c=c.caller;
}
}
return ev;
};
//獲取 事件源
this.getTarget=function(){
var ev=this.getEvent();
return this.isIe()?ev.srcElement:ev.target;
}
//是否為ie
this.isIe=function(){
return document.all?true:false;
}
//鼠標(biāo)x坐標(biāo)
this.clientX=function(){
var ev=this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//鼠標(biāo)y坐標(biāo)
this.clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**增加事件(對(duì)象,事件類型,函數(shù)指針 )
obj: html對(duì)象
sEvent: 事件名稱
spNotify: 事件執(zhí)行的方法
isCapture:是否允許全屏捕捉
*/
this.addEvent=function(obj,sEvent,fpNotify,isCapture){
sEvent=sEvent.indexOf("on")!=-1?sEvent:"on"+sEvent;
if(obj.addEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2);
obj.addEventListener(sEvent,fpNotify,isCapture);
}else{ //ie
if(isCapture)
obj.setCapture(isCapture);
obj.attachEvent(sEvent,fpNotify);
}
}
//移除事件
this.removeEvent=function(obj,sEvent,fpNotify){
if(obj.removeEventListener){
sEvent=sEvent.substring(sEvent.indexOf("on")+2)
obj.removeEventListener(sEvent,fpNotify,false);
}else{
obj.detachEvent(sEvent,fpNotify);
}
}
//獲取鼠標(biāo)按鍵,left=1,middle=2,right=3
this.button=function(){
var ev=this.getEvent();
if(!ev.which&&ev.button){//ie
return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return ev.which;
};
//阻止事件冒泡傳遞
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
else
ev.stopPropagation();
}
//阻止默認(rèn)事件返回
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}
相關(guān)文章
原生javascript實(shí)現(xiàn)勻速運(yùn)動(dòng)動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了原生javascript實(shí)現(xiàn)勻速運(yùn)動(dòng)動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02Bootstrap頁(yè)面布局基礎(chǔ)知識(shí)全面解析
Bootstrap作為支持響應(yīng)式布局的一個(gè)前端插件,發(fā)揮著非常重要的作用,下面通過本文給大家介紹Bootstrap頁(yè)面布局基礎(chǔ)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06Javascript attachEvent傳遞參數(shù)的辦法
找了半天找到的解決辦法,看介紹說是javascript的閉包問題,導(dǎo)致得不能直接讀取外部的那個(gè)函數(shù),不然就所有傳遞的參數(shù)都變?yōu)樽詈笠粋€(gè)了。2009-12-12微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車商品刪除功能
這篇文章主要介紹了微信小程序利用swiper+css實(shí)現(xiàn)購(gòu)物車商品刪除功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03微信小程序?qū)崿F(xiàn)列表項(xiàng)上移下移效果
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)列表項(xiàng)上移下移效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07JavaScript與Image加載事件(onload)、加載狀態(tài)(complete)
以前寫過一個(gè)圖片等比縮放的Js函數(shù),缺陷是要等到所有圖片都加載完畢了,才能進(jìn)行等比縮放。2011-02-02js或jquery實(shí)現(xiàn)頁(yè)面打印可局部打印
這篇文章主要介紹了js或jquery如何實(shí)現(xiàn)頁(yè)面打印也可局部打印,需要的朋友可以參考下2014-03-03