我最近在做一个项目的时候,需要写一个javascript的类来对一个ActiveX控件进行封装。而ActiveX控件有它自己的事件以及事件参数,我先后用了“attachEvent”和“$addHandler”来订阅事件,但都有不同的问题,“attachEvent”订阅的事件能正常得到参数,但“this”不再是当前类的实例。而用“$addHandler”订阅的事件能“this”能正确的返回当前类的实例,但ActiveX控件传过来的参数丢失了,而只有一个“Sys.UI.DomEvent”实例的参数。
为了满足需求我就把“$addHandler”方法做了如下的修改,让其能实现“this”和ActiveX控件的事件参数都能正确的得到,并且也保留了“Sys.UI.DomEvent”这个实例的参数。
1![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
var $addHandler = Sys.UI.DomEvent.addHandler = function Sys$UI$DomEvent$addHandler(element, eventName, handler) {
3
/// <summary locid="M:J#Sys.UI.DomEvent.addHandler" />
4
/// <param name="element"></param>
5
/// <param name="eventName" type="String"></param>
6
/// <param name="handler" type="Function"></param>
7
// var e = Function._validateParams(arguments, [
8
// { name: "element" },
9
// { name: "eventName", type: String },
10
// { name: "handler", type: Function }
11
// ]);
12
// if (e) throw e;
13
// Sys.UI.DomEvent._ensureDomNode(element);
14
if (eventName === "error") throw Error.invalidOperation(Sys.Res.addHandlerCantBeUsedForError);
15
if (!element._events) {
16
element._events = {};
17
}
18
var eventCache = element._events[eventName];
19
if (!eventCache) {
20
element._events[eventName] = eventCache = [];
21
}
22
var browserHandler;
23
if (element.addEventListener) {
24
browserHandler = function(e) {
25
var ev = null;
26
try {
27
ev = new Sys.UI.DomEvent(e);
28
} catch (ex) { }
29
var al = "handler.call(element, ev";
30
for (var i = 0; i < arguments.length; i++) {
31
al += ", arguments[" + i + "]";
32
}
33
al += ")";
34
return eval(al);
35
//return handler.call(element, ev, arguments);
36
}
37
element.addEventListener(eventName, browserHandler, false);
38
}
39
else if (element.attachEvent) {
40
browserHandler = function() {
41
var e = {};
42
try { e = Sys.UI.DomElement._getWindow(element).event } catch (ex) { }
43
var ev = null;
44
try {
45
ev = new Sys.UI.DomEvent(e);
46
} catch (ex) { }
47
var al = "handler.call(element, ev";
48
for (var i = 0; i < arguments.length; i++) {
49
al += ", arguments[" + i + "]";
50
}
51
al += ")";
52
return eval(al);
53
//return handler.call(element, ev, arguments);
54
}
55
element.attachEvent('on' + eventName, browserHandler);
56
}
57
eventCache[eventCache.length] = { handler: handler, browserHandler: browserHandler };
58
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)