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

首頁 > 語言 > JavaScript > 正文

自己的js工具 Event封裝

2024-05-06 14:14:16
字體:
來源:轉載
供稿:網友
因為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();
}
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 黑龙江省| 明光市| 仪陇县| 稷山县| 洪雅县| 巫溪县| 行唐县| 阿勒泰市| 黄陵县| 宜州市| 左云县| 六枝特区| 荔浦县| 扎鲁特旗| 岚皋县| 资溪县| 靖远县| 徐州市| 广饶县| 江川县| 盐亭县| 阳江市| 荣昌县| 鹤庆县| 晋江市| 平山县| 柯坪县| 佛冈县| 肥乡县| 昌宁县| 宁夏| 金阳县| 五河县| 栾城县| 宝山区| 莱芜市| 淄博市| 洛宁县| 安仁县| 西昌市| 光泽县|