Emptyform
@Emptyform

Как передается контекст в обработчике события?

Почему в этом коде, в функции showSpan контекст равен div.test ?
Что, jQuery сам налету биндит функцию showSpan при вызове, что ли?

$(document).ready(function() {
    function showSpan() {
        $(this).find('span').slideToggle();
    }

    $('div.test').on('mouseenter', 'li', showSpan);
});
  • Вопрос задан
  • 103 просмотра
Решения вопроса 1
zoonman
@zoonman
⋆⋆⋆⋆⋆
Да.

When jQuery calls a handler, the this keyword is a reference to the element where the event is being delivered; for directly bound events this is the element where the event was attached and for delegated events this is an element matching selector. (Note that this may not be equal to event.target if the event has bubbled from a descendant element.) To create a jQuery object from the element so that it can be used with jQuery methods, use $( this ).


https://api.jquery.com/on/
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы