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

后端 未结 6 486
梦谈多话
梦谈多话 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:10

    I know this has an accepted answer but if anyone comes upon this, my solution may help.

    I found this question because I have a use-case where I wanted to turn off the :hover state for elements individually. Since there is no way to do this in the DOM, another good way to do it is to define a class in CSS that overrides the hover state.

    For instance, the css:

    .nohover:hover {
        color: black !important;
    }
    

    Then with jQuery:

    $("#elm").addClass("nohover");
    

    With this method, you can override as many DOM elements as you would like without binding tons of onHover events.

提交回复
热议问题