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
}
i had wrote this before:
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;
}
}
}
someone know how jquery's work method on 'delegate' or 'on' for delegate?please show me the code simply description for 'delegate'..thanks alot.
When you do:
jQuery binds a handler to the event on the DOM elements that match
selector1
. When this handler runs, it walks the DOM hierarchy from the most specific element up to the element matchingselector1
, and checks whether any of the elements matchesselector2
. If it finds a match, it callsfunction
with the appropriate execution context.This is how
on()
is able to handle events on DOM elements that are added dynamically after the delegation is created.Have a look at the jQuery docs for
on()
, they explain the concept very well.Also, you can have a look at the source code!
The lessons learned:
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.With jQuery 1.4.2 launch, a new method called
delegate()
was introduced. This method attaches a handler to one or more events for selected/specified elements. Let's take an example. I have created a table and using delegate method, I will attach the click event handler to every td element.jQuery
delegate()
method code.It takes 3 arguments.
You will say that this is very much possible with the
bind()
method. Below code will serve the purpose.Then what's new with
delegate()
method?bind() method will add event to element which are on the page when it was called. For example, there are only 4 td on the page when bind() was called. Later on, when you dynamically add more td in the table then bind() will not attach click event handler to those td. Let's extend our demo and place a button on the page which will add the td dynamically.
Now, when you run this page, you will not find click event for newly added td.
But with
delegate()
, you will find click event for newly added td. delegate() method adds event which are on the page and also listens for new element and add event to them. :)Click a paragraph to add another. Note that .delegate() attaches a click event handler to all paragraphs - even new ones.
another example
working of methods for version added: 1.4.2
.delegate( selector, eventType, handler(eventObject) )
selector- A selector to filter the elements that trigger the event.
eventType- A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names.
handler(eventObject)- A function to execute at the time the event is triggered.
for version added: 1.4.2
delegate( selector, eventType, eventData, handler(eventObject) )
selector- A selector to filter the elements that trigger the event.
eventType- A string containing one or more space-separated JavaScript event types, such as "click" or "keydown," or custom event names. eventData A map of data that will be passed to the event handler.
handler(eventObject)- A function to execute at the time the event is triggered.
for version added: 1.4.3
delegate( selector, events )
selector- A selector to filter the elements that trigger the event.
events- A map of one or more event types and functions to execute for them.