Jquery - add class on hover

后端 未结 2 1273
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 11:37

I want when mouse goes on li jquery add a class to the element:

  • Menu Item
  • Menu Item
相关标签:
2条回答
  • 2021-01-07 11:48

    This will work, when you mouse over li it will add is_hovered to li and remove from other li.

    $(".menu li").mouseover(function(){
        $(".menu li").removeClass("is_hovered");
        $(this).addClass("is_hovered");
    });
    
    0 讨论(0)
  • 2021-01-07 12:04

    Don't use the mouseleave handler in that case, on mouseenter remove the hovered class from other elements

    var myMenu = $('.menu li');
    
    myMenu.mouseenter(function () {
        myMenu.filter('.is_hovered').removeClass("is_hovered");
        $(this).addClass("is_hovered");
    });
    

    Demo: Fiddle

    0 讨论(0)
提交回复
热议问题