May 2017
Intermediate to advanced
448 pages
10h 10m
English
Despite these efficiency concerns, there are reasons we may yet choose to use document as our delegation context. In general, we can only bind event handlers once the DOM elements they are attached to are loaded, which is why we typically place our code inside $(() => {}). However, the document element is available immediately, so we don't need to wait for the whole DOM to be ready before we bind to it. Even if the script is referenced in the <head> of the document, as it is in our example, we can call .on() right away, as follows:
(function($) { $(document) .on('mouseenter mouseleave', 'div.photo', (e) => { const $details = $(e.currentTarget).find('.details'); if (e.type == 'mouseenter') { $details.fadeTo('fast', 0.7); ...Read now
Unlock full access