可以用_evnets對象。Atlas爲每個被綁定了Dom事件的Dom元素添加了一個私有屬性_events對象。在你的例子中,_events[”click”]是一個對象數組,每個元素有2個屬性,例如:
$get(’clickControl’)._events[”click”][0]={handler:A,browserHandler:(內容省略……)}
其中browserHandler是爲了處理瀏覽器兼容性問題而定義的一個function,是實際被綁定的EventHandler,對於不同的瀏覽器browserHandler會有所不同,有興趣可以去看看源碼。這樣的話就可以通過看$get(’clickControl’)._events[”click”]數組中是否有元素的handler屬性爲B來判斷B是否被綁定了……好像有點繞口-_-!不過應該能理解了吧……
當然,還有一個辦法就是自己給被綁定了Dom事件的Dom元素添加一個屬性,用來記錄綁定了那些事件和處理函數……
這是網友dudu關於我的問題的回答,很感謝.
我想最後我還是選擇了同你的第2種方法.
我是這樣寫的
Sys.UI.seachControl = function Sys$UI$seachControl(
element,
)
{
/// <param name="element" domElement="true"></param>
var e = Function._validateParams(arguments, [{name: "element", domElement: true} ]);
if (e) throw e;
if (typeof(element.control) != 'undefined') throw Error.invalidOperation(Sys.Res.controlAlreadyDefined);
Sys.UI.seachControl.initializeBase(this,[element]);
this._element = element;
element.control = this;
this._oldDisplayMode = this._element.style.display;
if (!this._oldDisplayMode || (this._oldDisplayMode == 'none')) {
this._oldDisplayMode = '';
}
this.is_bind_click = false;
this.is_bind_mouseover = false;
this.is_bind_mouseout = false;
this.$addSelectHandler = function(Ele,evt,handler,bind)
{
switch(evt)
{
case "click":
if(!this.is_bind_click)
{
$addHandler(Ele,evt,handler)
this.is_bind_click = bind ;
}
break;
case "mouseover":
if(!this.is_bind_mouseover)
{
$addHandler(Ele,evt,handler)
this.is_bind_mouseover = bind ;
}
break;
case "mouseout":
if(!this.is_bind_mouseout)
{
$addHandler(Ele,evt,handler)
this.is_bind_mouseout = bind ;
}
break;
}
}
this.$removeSelectHandler = function(Ele,evt,handler,bind)
{
switch(evt)
{
case "click":
if(this.is_bind_click)
{
$removeHandler(Ele,evt,handler)
this.is_bind_click = bind ;
}
break;
case "mouseover":
if(this.is_bind_mouseover)
{
$removeHandler(Ele,evt,handler)
this.is_bind_mouseover = bind ;
}
break;
case "mouseout":
if(this.is_bind_mouseout)
{
$removeHandler(Ele,evt,handler)
this.is_bind_mouseout = bind ;
}
break;
}
}
}
Sys.UI.seachControl.registerClass('Sys.UI.seachControl', Sys.UI.Control);
於是,就可以用它給事件添加處理函數和註銷處理函數,不用擔心會給一個事件添加添加同名的處理函數,引起混亂了.呵呵
_events屬性來自於繼承的Component類的get_events獲得一個EventHandlerList對象
在EventHandlerList中有_getEvent(id)//id應該大概是事件類型//的方法,這返回應該是個一維數組了吧.
可是我注意到,EventHandlerList類還有一個getHandler(id)的方法將會得到下面的處理函數代碼字符串
function(source, args) {
for (var i = 0, l = evt.length; i < l; i++) {
evt[i](source, args);
}
};
如果這樣寫
var handler = this._events.getHandler("click");
handler(source, args);
才能使這個處理函數立即執行.但我不知道sourc和eargs參數應該傳遞什麼纔對.
從這裏也說明了,如果使用得到的EventHandlerList對象的addHandler(id,handler)和removeHandler(id,handler)方法添加與刪除事件處理函數就不象Sys.UI.DomEvent.addHandler(Element,event,handler)和Sys.UI.DomEvent.removeHandler(Element,event,handler),即$addHandler(Element,event,handler)和$removeHandlerElement,event,handler)一樣是添加的處理函數執行.
甚至,說如果不能用
var handler = this._events.getHandler("click");
handler(source, args);
這個事件處理函數永遠不會被執行一樣.
不曉得大家怎麼看,另外source, args兩個參數傳什麼?