This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.js
117 lines (100 loc) · 3.93 KB
/
event.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var Event = function () {
this.attach = function (evtName, element, listener, capture) {
var evt = '',
useCapture = (capture === undefined) ? true : capture,
handler = null;
if (window.addEventListener === undefined) {
evt = 'on' + evtName;
handler = function (evt, listener) {
element.attachEvent(evt, listener);
return listener;
};
} else {
evt = evtName;
handler = function (evt, listener, useCapture) {
element.addEventListener(evt, listener, useCapture);
return listener;
};
}
return handler.apply(element, [evt, function (ev) {
var e = ev || event,
src = e.srcElement || e.target;
listener(e, src);
}, useCapture]);
};
this.detach = function (evtName, element, listener, capture) {
var evt = '',
useCapture = (capture === undefined) ? true : capture;
if (window.removeEventListener === undefined) {
evt = 'on' + evtName;
element.detachEvent(evt, listener);
} else {
evt = evtName;
element.removeEventListener(evt, listener, useCapture);
}
};
this.stop = function (evt) {
evt.cancelBubble = true;
if (evt.stopPropagation) {
evt.stopPropagation();
}
};
this.prevent = function (evt) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
};
};
/* --------------------------邪恶的分割线--------------------------- */
var click = document.getElementById('click'),
multipleEvents = document.getElementById('multiple-events'),
multipleListeners = document.getElementById('multiple-listeners'),
noevent = document.getElementById('no-event'),
input = document.getElementById('prevent-click'),
once = document.getElementById('once'),
upper = document.getElementById('upper'),
lower = document.getElementById('lower'),
evt = new Event(),
message = function (e, src) {
window.alert(e.type + ' event on "' + src.id + '"');
},
messageHTML = function (e, src) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += '<div>' + e.type + ' event on "' + src.id + '"</div>';
infoElement.scrollTop = infoElement.scrollHeight;
},
messageMulti = function (e, src, lName) {
var infoElement = document.getElementById(src.id + '-info');
infoElement.innerHTML += '<div>' + e.type + ' event on "'
+ src.id + ' with "' + lName + '"</div>';
infoElement.scrollTop = infoElement.scrollHeight;
};
evt.attach('click', document.getElementById('click'), message, false);
evt.attach('mouseover', multipleEvents, messageHTML);
evt.attach('mouseout', multipleEvents, messageHTML);
evt.attach('mousedown', multipleEvents, messageHTML);
evt.attach('mouseup', multipleEvents, messageHTML);
var noeventHandler = evt.attach('mousedown', noevent, message);
evt.detach('mousedown', noevent, noeventHandler);
var onceHandler = evt.attach('mousedown', once, function (e, src) {
messageHTML(e, src);
evt.detach('mousedown', once, onceHandler);
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 1');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 2');
});
evt.attach('click', multipleListeners, function (e, src) {
messageMulti(e, src, 'listener 3');
});
evt.attach('keypress', input, function (e) {
evt.prevent(e);
});
evt.attach('mousedown', upper, function (e, src) {
evt.stop(e);
alert(src.id);
});