自己的js工具 Event封装
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了自己的js工具 Event封装,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2174字,纯文字阅读大概需要4分钟。
内容图文
![自己的js工具 Event封装](/upload/InfoBanner/zyjiaocheng/374/e90701174595485b8fe284be261be0a5.jpg)
代码如下:
/**
类 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();
}
}
内容总结
以上是互联网集市为您收集整理的自己的js工具 Event封装全部内容,希望文章能够帮你解决自己的js工具 Event封装所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。