remove css :hover attribute with jquery

前端 未结 7 1020
遇见更好的自我
遇见更好的自我 2020-12-10 02:44

Lets say I have


相关标签:
7条回答
  • 2020-12-10 02:56

    You can't remove pseudo-class rules, but you can override them with event bindings:

    $("#container").on('mouseover', function () {
       $(this).css('background', 'blue');
    }).on('mouseout', function () {
       $(this).css('background', whateverItIsNormally);
    });
    
    0 讨论(0)
  • 2020-12-10 02:57

    Depending on your browser pointer-events:none might help

    https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events

    0 讨论(0)
  • 2020-12-10 03:07

    This may be a bit convoluted and could certainly use optimization, but you could use a combination of things posted so far:

    jsFiddle: http://jsfiddle.net/psyon001/Vcnvz/2/

    <style>
        .red{background:red;}
    </style>
    
    <div id="container"><input type="submit"></div>
    
    <script>
    $('#container').on({
        'mouseenter' : function(){
            $(this).addClass('red');
        },
        'mouseleave' : function(){
            $(this).removeClass('red');
        }
    });
    $('#container').find('input').on({
        'mouseenter' : function(){
            $('#container').removeClass('red');
        },
        'mouseleave' : function(){
            $('#container').addClass('red');
        }
    })
    </script>
    
    0 讨论(0)
  • 2020-12-10 03:09

    I think this is better

    <style>
        .red:hover { background-color:red; }
    </style>
    <!-- by default is On-->
    <div class="box red"></div>
    <script>
        $('.box').click(function(){
            $(this).toggleClass('red');
        });
    </script>
    
    0 讨论(0)
  • 2020-12-10 03:10

    Unfortunately it is impossible to manage CSS pseudo-classes with jQuery.

    I'd suggest you to manipulate classes, as follows:

    <style>
        .red:hover{background:red}
    </style>
    
    <div id="container" class="red"><input type="submit"></div>
    
    <script>
        $("#container").removeClass("red");
    </script>
    
    0 讨论(0)
  • 2020-12-10 03:12

    I applied this:

    element.click(function(){
      element.hover(
        function(){
         element.css('background-color', '');
         element.css('color', '');
      },
        function(){
         element.css('background-color', '');
         element.css('color', '');
      });
    });
    

    And it seemed to work in removing the hover properties while retaining the original CSS.

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