-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventProcess.js
More file actions
169 lines (144 loc) · 3.24 KB
/
Copy pathEventProcess.js
File metadata and controls
169 lines (144 loc) · 3.24 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*window.onload = function(){
alert('a');
alert('b');
alert('c');
};*/
/*addEvent(window,'load',function(){
alert('a');
});
addEvent(window,'load',function(){
alert('a');
});
addEvent(window,'load',function(){
alert('a');
});
window.onload = function(){
var oButton = document.getElementById(('btn');
addEvent(oButton,'click',fn);
removeEvent(oButton,'click',fn);
//addEvent(oButton,'click',fn);
//addEvent(oButton,'click',fn);
};
function fn(){
aler('button');
}*/
/*
window.onload = function(){
var oButton = document.getElementById(('btn');
addEvent(oButton,'click',fn);
//removeEvent(oButton,'click',fn);
};
function fn(e){
//aler(e.clientX);
alert(this.value);
}
function addEvent(obj,type,fn){
if (typeof obj.addEventListener != 'undefined'){//W3C
obj.addEventListener(type,fn,false);
}else if (typeof obj.attchEvent != 'undefined'){//IE
obj.attachEvent('on' + type,function(){
fn.call(obj,window.event);
});
}
}
function removeEvent(obj,type,fn){
if (typeof obj.removeEventListener != 'undefined'){//W3C
obj.removeEventListener(type,fn,false);
}else if (typeof obj.detachEvent != 'undefined'){//IE
obj.detachEvent('on' + type,fn);
}
}
*/
//全局变量
// var id = 1;
//属性
addEvent.ID = 1;
function addEvent(obj,type,fn){
if (typeof obj.addEventListener != 'undefined'){//W3C
obj.addEventListener(type,fn,false);
}else{//IE
addEvent.ID++;
if (!obj.events){
obj.events = {};
}
if (!obj.events[type]){
obj.events[type] = [];
if (obj['on'+type]){
obj.events[type][0] = fn;
}else{
if(addEvent.equal(obj.events[type],fn)){
return false;
}
}
}
obj.events[type][addEvent.ID++] = fn;
obj['on'+type] = addEvent.exec;
}
}
addEvent.exec = function(event){
var e = event||addEvent.fixEvent(window.event);
for(var i in this.events[e.type]){
this.events[e.type][i].call(this,e);
}
}
addEvent.equal = function(es,fn){
for(var i in es){
if(es[i] == fn){
return true;
}
}
return false;
}
//ie event match W3C
addEvent.fixEvent = function(event){
event.preventDefault = addEvent.fixEvent.preventDefault;
event.stopPropagation = addEvent.fixEvent.stopPropogation;
return event;
}
addEvent.fixEvent.preventDefault = function(){
this.returnValue = false;
}
addEvent.fixEvent.stopPropogation = function(){
this.returnValue = true;;
}
function removeEvent(obj,type,fn){
if (typeof obj.removeEventListener != 'undefined'){//W3C
obj.removeEventListener(type,fn,false);
}else {//IE
for(var i in obj.events[type]){
if (obj.events[type][i] == fn){
delete obj.events[type][i];
}
}
}
}
window.onload = function(){
/*var oButton = document.getElementById(('btn');
addEvent(oButton,'click',fn1);
//removeEvent(oButton,'click',fn1);
addEvent(oButton,'click',fn2);
//removeEvent(oButton,'click',fn2);
addEvent(oButton,'click',fn3);
//removeEvent(oButton,'click',fn3); */
var a = document.getElementById(('a');
addEvent(a,'click',function(e){
e.preventDefault();
//preDef(e);
});
addEvent(oButton,'click',function(e){
e.stopPropogation();
//....do what you want
});
addEvent(document,'click',function(){
//..do what you want
});
};
function fn1(e){
alert('1' + this.value + e.clientX);
}
function fn2(e){
alert('2');
}
function fn3(e){
alert('3');
}