如何在jQuery悬停事件处理程序中使用$(this)?

edw*_*ard 3 javascript jquery this

我在div中隐藏了一个无序列表.div有一个'.feed-label'类,当div跳过时我正在显示ul.

我的问题是,当盘旋时,所有其他元素也都显示出来,我只想要悬停在其中的那个元素来显示.

我不知道如何使用$(this).

$('.feed-label').hover(function() {
    $('.article-interactive-buttons').toggleClass('hide');
});
Run Code Online (Sandbox Code Playgroud)

und*_*ned 7

this事件处理程序上下文中的关键字引用了悬停元素,即.feed-label元素.您应该通过将元素传递给jQuery构造函数来创建jQuery对象,然后使用find/ children方法选择目标后代.

$('.feed-label').hover(function() {
    $(this).find('.article-interactive-buttons').toggleClass('hide');
});
Run Code Online (Sandbox Code Playgroud)

您还可以使用$(selector, context)与上述代码段类似的语法:

$('.feed-label').hover(function() {
    $('.article-interactive-buttons', this).toggleClass('hide');
});
Run Code Online (Sandbox Code Playgroud)

  • 哇,我想知道这些年来我是如何错过`selector,context`语法的.棒极了. (2认同)