ホームページ  >  記事  >  ウェブフロントエンド  >  独自jsツール イベントパッケージング_javascriptスキル

独自jsツール イベントパッケージング_javascriptスキル

WBOY
WBOYオリジナル
2016-05-16 18:47:421148ブラウズ

IE のイベントはグローバルであり、Firefox のイベントはローカルであるため、現時点では、よく使用されるイベント操作を自分で組み立て、再利用しやすいようにクラスにカプセル化する必要があります。

コードをコピーします コードは次のとおりです:

/**
クラス イベント
使用法:
Event.getEvent(); ie、firefox のイベントを取得します。
Event.getTarget(); ie の srcElement または firefox
Event のターゲットを取得します。 isIe() ; ie かどうか
Event.clientX(); ie,fox
Event.clientY() のマウス 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;
ev=c.arguments[0]; 🎜>if(ev && Event = =ev.constructor)
break
c=c.caller;
}
/ /イベントソースを取得
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; 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); ,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);
// マウスを取得します。ボタン、左 = 1、中央 = 2、右 = 3
this.button=function(){
var ev=this.getEvent();
if(!ev.that&&ev.button){/ /ie
return ev.button&1?1:(ev.button&2?3:(ev.button&4?2:0))
}
return
}; /イベントのバブリング配信を防止します
this.stopPropagation=function(){
var ev=this.getEvent();
if(this.isIe)
ev.cancelBubble=true;
ev.stopPropagation( );
}
// デフォルトのイベントが返されないようにする
this.preventDefault=function(){
var ev=this.getEvent(); if(this.isIe)
ev.returnValue=false;
else
ev.preventDefault()
}
}

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。