How to define the css :hover state in a jQuery selector?

后端 未结 6 513
梦谈多话
梦谈多话 2020-11-30 04:10

I need to define a div\'s background color on :hover with jQuery, but the following doesn\'t seem to work:

$(\".myclass:hover div\").css(\"background-color\         


        
6条回答
  •  醉酒成梦
    2020-11-30 05:13

    I would suggest to use CSS over jquery ( if possible) otherwise you can use something like this

    $("div.myclass").hover(function() {
      $(this).css("background-color","red")
    });
    

    You can change your selector as per your need.

    As commented by @A.Wolff, If you want to use this hover effect to multiple classes, you can use it like this

    $(".myclass, .myclass2").hover(function(e) { 
        $(this).css("background-color",e.type === "mouseenter"?"red":"transparent") 
    })
    

    Js Fiddle Demo

提交回复
热议问题