自己的js工具 Event封装
作者:bea
因为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坐
因为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();
}
}
有用 | 无用
代码如下:
/**
类 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();
}
}
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 动态表格Table类的实现
- javascript 单行文字向上跑马灯滚动显示
- javascript 函数调用规则
- JSON 入门指南 想了解json的朋友可以看下
- javascript 继承实现方法
- 监控用户是否关闭浏览器的js代码
- javascript 随机广告代码(图片广告)
- JS去除字符串的空格增强版(可以去除中间的空格)
- 可以读取EXCEL文件的js代码
- JavaScript 获取用户客户端操作系统版本
- JS 获取span标签中的值的代码 支持ie与firefox
- jquery 表单进行客户端验证demo
- JS 时间显示效果代码
- JQuery this 和 $(this) 的区别
- 可拖动可改变大小div的实现代码
- JavaScript 语言精粹学习笔记
- 图片展示效果 鼠标经过变大图,支持FF
- ext checkboxgroup 回填数据解决
- 获取HTML DOM节点元素的方法的总结