国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

自己的js工具 Event封裝

2019-11-21 01:11:22
字體:
來源:轉載
供稿:網友
因為ie的event是全局的而firefox的event是局部的,用起來不太方便,這個時候我們就要自己組裝一下常用的event操作了,封裝成類便于重用
復制代碼 代碼如下:

/**
類 Event
用法:
Event.getEvent();獲取 ie,firefox的event
Event.getTarget();獲取ie的srcElement或firefox的target
Event.isIe();是否為ie
Event.clientX(); 獲取ie,fox的鼠標x坐標
Event.clientY();獲取 ie,fox的鼠標y坐標
*/
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;
}
//鼠標x坐標
this.clientX=function(){
var ev=this.getEvent();
var x=this.isIe()?ev.clientX:ev.pageX;
return x;
}
//鼠標y坐標
this.clientY=function(){
var ev=this.getEvent();
var y=this.isIe()?ev.clientY:ev.pageY;
return y;
}
/**增加事件(對象,事件類型,函數指針 )
obj: html對象
sEvent: 事件名稱
spNotify: 事件執行的方法
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);
}
}
//獲取鼠標按鍵,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();
}
//阻止默認事件返回
this.preventDefault=function(){
var ev=this.getEvent();
if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault();
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 茂名市| 财经| 宜州市| 石阡县| 漯河市| 通许县| 濉溪县| 开阳县| 抚松县| 靖远县| 克东县| 镇安县| 垦利县| 谢通门县| 余庆县| 田阳县| 汉川市| 饶阳县| 洛扎县| 稷山县| 屏东县| 商水县| 巴青县| 中超| 门源| 开封县| 确山县| 金堂县| 轮台县| 兴安盟| 滕州市| 江陵县| 达州市| 渭南市| 富民县| 泸溪县| 商河县| 汕头市| 德阳市| 巫溪县| 金塔县|