Just One Of Those Things You Need To Understand About JavaScript

/just-one-of-those-things-you-need-to-un

  • Just One Of Those Things You Need To Understand About JavaScript | CSS-Tricks
    http://css-tricks.com/just-one-of-those-things-you-need-to-understand-about-javascript

    While this is “just how JavaScript works” it’s a known pain in the butt. Since jQuery is a library that exists to ease pains like this, of course it has a better way. That way is the .delegate() function, where instead of binding events to the individual elements, you bind an event to an element higher up the DOM tree, which isn’t likely to be replaced via Ajax, which watches for those clicks.

    This relies on something called event bubbling, which a neat and important concept in JavaScript. If you click on a thumbnail, it will trigger a click event on that element, then a click event on it’s parent element, and a click event on it’s parent’s parent element, all the way up to the root element. Because of this, we can watch for clicks on deeper-down elements from higher-up elements.

    Je trouve que ça n’explique pas vraiment la différence entre .delegate() et .live(), la doc officielle de #jQuery n’est pas non plus super-claire.

    Si quelqu’un a de la doc sur les spécificités de .delegate(), ça me branche.