jquery - 鼠标悬停事件后 addClass() 不起作用

标签 jquery

大家好,当我使用 jquery 添加类时,添加的类不起作用

$('.t_wrapper').mouseover(function () {
    $(this).addClass('t_active');
});
$('.t_wrapper').mouseout(function () {
    $('.t_wrapper').removeClass('t_active');
});

//'.t_active' 无法识别父标签,抱歉我的英语:(

var tag = $('.t_active .tables li:nth-child(' + n + ')');

最佳答案

选项 1:

 $('.t_wrapper').mouseenter(function () {
      $(this).addClass('t_active');
 });
 $('.t_wrapper').mouseleave(function () {
      $(this).removeClass('t_active');
 });

选项2

 $('.t_wrapper').mouseover(function () {
      $('.t_wrapper').addClass('t_active');
 });
 $('.t_wrapper').mouseout(function () {
      $('.t_wrapper').removeClass('t_active');
 });

每次鼠标进入或离开子元素时,都会触发 mouseover,但不会触发 mouseenter。 因此,不要在 mouseover 或 mouseout 中使用 $(this),它们不是同一目标。

关于jquery - 鼠标悬停事件后 addClass() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786644/

相关文章:

php - 将 WHERE 函数与 $_POST 一起使用

php - 使用ajax和PHP上传图像

javascript - jquery ajax 调用的正确语法

JQuery Mobile 分组按钮宽度 - 使用 window.resize 跨越 100% 的 DIV

javascript - 如何将表单数据从 jQuery 获取到 ASP.NET Controller ?

javascript - require.js 与 jquery ui,找不到 jquery

javascript - 用JS改变一个div的多个属性

javascript - Bootstrap-table - data-reactid 属性的排序不正确

jquery - FullCalendar - 在议程 View 中删除时间轴?

jquery - 如何在 Svelte 中使用 jQuery