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

后端 未结 6 479
梦谈多话
梦谈多话 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 04:59

    You can try this:

    $(".myclass").mouseover(function() {
        $(this).find(" > div").css("background-color","red");
    }).mouseout(function() {
        $(this).find(" > div").css("background-color","transparent");
    });
    

    DEMO

    0 讨论(0)
  • 2020-11-30 04:59

    Well, you can't add styling using pseudo selectors like :hover, :after, :nth-child, or anything like that using jQuery.

    If you want to add a CSS rule like that you have to create a <style> element and add that :hover rule to it just like you would in CSS. Then you would have to add that <style> element to the page.

    Using the .hover function seems to be more appropriate if you can't just add the css to a stylesheet, but if you insist you can do:

    $('head').append('<style>.myclass:hover div {background-color : red;}</style>')

    If you want to read more on adding CSS with javascript you can check out one of David Walsh's Blog posts.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 05:10

    Use JQuery Hover to add/remove class or style on Hover:

    $( "mah div" ).hover(
      function() {
        $( this ).css("background-color","red");
      }, function() {
        $( this ).css("background-color",""); //to remove property set it to ''
      }
    );
    
    0 讨论(0)
  • 2020-11-30 05:12

    It's too late, however the best example, how to add pseudo element in jQuery style

     $(document).ready(function(){
     $("a.dummy").css({"background":"#003d79","color":"#fff","padding": "5px 10px","border-radius": "3px","text-decoration":"none"});
     $("a.dummy").hover(function() {
                $(this).css("background-color","#0670c9")
              }).mouseout(function(){
                  $(this).css({"background-color":"#003d79",});
              });
     
     });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <a class="dummy" href="javascript:void()">Just Link</a>

    0 讨论(0)
  • 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

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