May 2017
Intermediate to advanced
448 pages
10h 10m
English
Event delegation can be frustratingly difficult to manage by hand when tasks become more complex. Fortunately, jQuery's .on() method has delegation built into it, which can make life easier for us. Using this capability, our code can return to the simplicity of Listing 10.4:
$(() => { $('#gallery') .on('mouseenter mouseleave', 'div.photo', (e) => { const $details = $(e.currentTarget).find('.details'); if (e.type == 'mouseenter') { $details.fadeTo('fast', 0.7); } else { $details.fadeOut('fast'); } });});
The selector, #gallery, remains the same as in Listing 10.5, but the event types return to the mouseenter and mouseleave of Listing 10.4. When we pass in 'div.photo' as the second argument ...
Read now
Unlock full access