event-delegation

jquery Event Delegation

社会主义新天地 提交于 2019-12-02 08:14:45
I am attempting to rewrite a snippet of code using event delegation (in the hopes that it will stop conflict with another snippiet of js). but I've broken the code Original //to scale up on hover var current_h = null; var current_w = null; $('.piccon').hover( function(){ current_h = $(this, 'img')[0].height; current_w = $(this, 'img')[0].width; $(this).stop(true, false).animate({width: (current_w * 2.7), height: (current_h * 2.7)}, 900); }, function(){ $(this).stop(true, false).animate({width: current_w + 'px', height: current_h + 'px'}, 400); } ); //using event delegation //to scale up on

jQuery delegate performance on the click event on large lists - slows down if you dynamically add more elements?

主宰稳场 提交于 2019-12-02 01:33:08
I have a visual list of items like this: http://jsfiddle.net/viatropos/XCe3T/1/ In the real app, I'm only loading 200 total items. But the problem is the click event takes almost one second to call the handler, even with just 200 items. The mouseover event callback executes immediately no matter how many items are in the list. My question is, shouldn't the delegate method be just as fast no matter how many elements are on the page? All I am doing is this: $("body").delegate("a", "click", function(event) { console.log($(event.target).get(0)); return false; } If you go to that jsfiddle example

Moving from classic event management to event delegation in JavaScript OOP

穿精又带淫゛_ 提交于 2019-12-01 12:34:14
The old event management in which each handler for specific actions was directly attached to the target element is becoming outdated, since considerations about performance and memory saving started spreading in the developers community. Event delegation implementations had an acceleration since jQuery updated the old fashioned .bind() and .live() methods with the new .on() method to allow delegation. This determines a change in some seasoned approaches, where to use event delegation a rework is necessary. I am trying to work out some best practice while keeping the coding style of my library,

Moving from classic event management to event delegation in JavaScript OOP

余生长醉 提交于 2019-12-01 11:57:42
问题 The old event management in which each handler for specific actions was directly attached to the target element is becoming outdated, since considerations about performance and memory saving started spreading in the developers community. Event delegation implementations had an acceleration since jQuery updated the old fashioned .bind() and .live() methods with the new .on() method to allow delegation. This determines a change in some seasoned approaches, where to use event delegation a rework

Remove jQuery delegated event handler on specific object

自古美人都是妖i 提交于 2019-11-29 11:08:28
I've attached delegated event handlers to a number of elements on the page using a single selector. As the events are triggered for individual elements, I'd like to turn off only that element's event handler based on some conditional logic . That means I won't necessarily want to disable the event on the very first click . But I can't figure out how to do it without turning off all of them. HTML: <button>One</button> <button>Two</button> <button>Three</button> JS: $(document).on('click', 'button', function(ev) { // doesn't work because argument needs to be a string $(document).off('click', $

Remove jQuery delegated event handler on specific object

六眼飞鱼酱① 提交于 2019-11-28 04:26:59
问题 I've attached delegated event handlers to a number of elements on the page using a single selector. As the events are triggered for individual elements, I'd like to turn off only that element's event handler based on some conditional logic . That means I won't necessarily want to disable the event on the very first click . But I can't figure out how to do it without turning off all of them. HTML: <button>One</button> <button>Two</button> <button>Three</button> JS: $(document).on('click',

Event delegation in jQuery, how to do it?

荒凉一梦 提交于 2019-11-28 02:25:37
In JavaScript i used to use event delegation like this : someDiv.addEventListener('click', function(evt){ if(evt.target.id == "#someChild"){ // Do something.. } else if(evt.target.id == "#anotherChild"){ // Do something else.. } }, false); What's the equivalent of this in jQuery? i know about .on() but how to use it in event delegation ? i mean is this how is it done : someDiv.on('click, '#someChild, #anotherChild", function(evt){ if($(this).is("#someChild")){ // Do something.. } else if($(this).is("#anotherChild")){ // Do something else .. } }); But wouldn't it be just the same as in vanilla

Native JS equivalent to jquery delegation

只愿长相守 提交于 2019-11-27 14:26:55
What is the native implementation for event delegation on dynamically created dom elements? I tried looking at the jquery source but I cant follow the .on method. Note: Currently I attach the event handlers after the dom elements are created, which seems pretty standard but I like the way jquery .on handles dynamically created elements events with this syntax $( document ).on( "click", ".selector", handler ); What happens is basically this: // $(document).on("click", <selector>, handler) document.addEventListener("click", function(e) { for (var target=e.target; target && target!=this; target

How does jQuery.on() work?

不羁的心 提交于 2019-11-27 06:03:50
问题 I have not seen the source of this function, but I wonder, does it work like this: Selects element(s) by their selecotrs Delegates the provided event-handlers to them Runs a setInterval on that selector and constantly un-delegating, and then re-delegating that same event all over Or there is a pure-JavaScript DOM explanation to this? 回答1: I assume your question is about the Event Delegation version of .on . In JavaScript, most events bubble up in the DOM hierarchy. That means, when an event

Vanilla JS event delegation - dealing with child elements of the target element

旧时模样 提交于 2019-11-27 04:38:09
I'm trying to do event delegation in vanilla JS. I have a button inside a container like this <div id="quiz"> <button id="game-again" class="game-again"><span class="icon-spinner icon"></span><span>Go again</span></button> </div> And following David Walsh's nice instructions I'm adding an event handler to an ancestor of the button like so: this.container.addEventListener('click', function(e){ if (e.target && e.target.id == 'game-again') { e.stopPropagation(); self.publish('primo:evento'); } }); Where this.container is the #quiz element. This works half the time, but the rest of the time the