我想知道jquery的委托或代理(代表)如何工作 [英] i want to know how jquery' delegate or on(for delegate) works
本文介绍了我想知道jquery的委托或代理(代表)如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有时我使用来委托事件,
sometimes i use on
to delegate event ,
dom.addEventListener("click",function(e){
e.target for hander.
}
instead:
dom.on("click",'a',function(){
$(this).handler..
}
我想我可以这样编写代码:
so,i guess i can write codes in this way :
function delegate(dom,event,selector,handler){
target = event.target;
while selector.dom.not_match event.target
target = target.parentNode
recheck until match the selector and do handler;
end
}
我以前写过:
function delegate(dom,event,selector,handler){
dom.addEvent event function(){
target_arr = dom.find(selector);
if(event.target in_array target_arr){
do handler
}else{
target = target.parentNode until dom.
recheck in target_arr;
}
}
}
有人知道jquery的工作方法代理或开代表委托?请给我代码简单的代理的描述..谢谢alot。
someone know how jquery's work method on 'delegate' or 'on' for delegate?please show me the code simply description for 'delegate'..thanks alot.
推荐答案
p>查看 jQuery文档 on( )
,他们非常好地解释了这个概念。
Have a look at the jQuery docs for on()
, they explain the concept very well.
另外,你可以看看源代码!
经验教训:
-
委托
只是一个包含在
上的不同参数的包装订单 -
on
只是一些参数归一化并处理一个
,然后委托给jQuery.event.add(this,types,fn,data,selector);
-
event.add
做了大量的验证,处理多种类型和特殊情况,推送$。data(events)上的参数,
并调用elem.addEventListener(type,jQuery.event.dispatch,false)
-
event.dispatch
然后从$。data(events)
再次查询句柄,并构建一个jqEvent
。然后,它开始搜索委派事件 - 该代码是非常简单的,并将它们推送到handlerQueue
之后,直接附加在元素上的普通处理程序。最后,它只是运行handlerQueue
,从委托处理程序开始。
delegate
is just a wrapper foron
with different parameter orderon
does just some parameter normalisation and handlesone
, but delegates then tojQuery.event.add( this, types, fn, data, selector );
event.add
does do a lot of validation, handles multiple types and special cases, pushes the arguments on$.data("events")
and callselem.addEventListener(type, jQuery.event.dispatch, false)
event.dispatch
then queries the handles from$.data("events")
again and builds ajqEvent
from the native event. Then it begins searching for delegated events - the code for that is quite straightforward - and pushes them on thehandlerQueue
, after that the normal handlers which are attached directly on the element. In the end, it just runs thehandlerQueue
, starting with the delegated handlers.
这篇关于我想知道jquery的委托或代理(代表)如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文