<スクリプトタイプ="text/javascript">
関数 EventUtil() {
var _self = this;
///追加イベント
var addEvent = (function () {
if (document.addEventListener) {
戻り関数 (el、type、fn) {
el.addEventListener(type, fn, false);
}
} else {
戻り関数 (el、type、fn) {
el.attachEvent("on" type, function () {
return fn.call(el, window.event);
});
}
}
})();
///プロパティ変更イベントを追加
var addPropertyChangeEvent = function (obj, fn) {
if (window.ActiveXObject) {
obj.onpropertychange = fn;
} else {
obj.addEventListener("input", fn, false);
}
}
//削除イベント
varmoveEvent = function (obj, type, fn) {
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
else if (obj.detachEvent) {
obj.detachEvent("on" タイプ, obj["on" type fn]);
obj["on" type fn] = null;
}
}
//イベントを追加
varloadEvent = function (fn) {
var oldonload = window.onload;
if (typeof oldonload != "関数") {
window.onload = fn;
} else {
window.onload = function () {
oldonload();
fn();
}
}
}
//イベントを阻止します
var stopEvent = function (e) {
e = e || window.event;
if (e.preventDefault) {
e.preventDefault();
e.stopPropagation();
} else {
e.returnValue = false;
e.cancelBubble = true;
}
}
//イベントのバブリングを防ぎたいだけの場合
var stopPropagation = function (e) {
e = e || window.event;
if (! "v1") {
e.cancelBubble = true;
} else {
e.stopPropagation();
}
}
//イベントソースオブジェクトを取得します
var getEvent1 = function (e) {
e = e || window.event;
var obj = e.srcElement : e.target;
return obj;
}
//イベントソースオブジェクトを取得します
var getEvent2 = function (e) {
If (window.event) return window.event;
var c = getEvent2.caller;
while (c.caller) {
c = c.caller;
}
return c.arguments[0];
}
// あるいは、この関数の方が強力です
var getEvent3 = function (e) {
var e = e || window.event;
if (!e) {
var c = this.getEvent3.caller;
一方 (c) {
e = c.arguments[0];
If (e && (Event == e.constructor || MouseEvent == e.constructor)) {
休憩;
}
c = c.caller;
}
}
var target = e.srcElement ? e.srcElement : e.target,
currentN = target.nodeName.toLowerCase(),
parentN = target.parentNode.nodeName.toLowerCase(),
grandN = target.parentNode.parentNode.nodeName.toLowerCase();
return [e, target, currentN, parentN, grandN];
}
_self.addEvent = addEvent;
_self.addPropertyChangeEvent = addPropertyChangeEvent;
_self.removeEvent = removeEvent;
_self.loadEvent = loadEvent;
_self.stopEvent = stopEvent;
_self.stopPropagation = stopPropagation;
_self.getEvent1 = getEvent1;
_self.getEvent2 = getEvent2;
_self.getEvent3 = getEvent3;
}
var eventUtil = new EventUtil();
eventUtil.loadEvent(function () {
eventUtil.addEvent(document, "click", function (e) {
alert(eventUtil.getEvent3(e));
});
eventUtil.addPropertyChangeEvent(document,function(e){
alert(eventUtil.getEvent3(e));
});
});