2012-12-05 61 views
15

悬停属性可以说我有删除CSS:用jQuery

<style> 
    #container:hover{background:red} 
</style> 
<div id="container"><input type="submit"></div> 

当我将鼠标悬停在提交,#container仍为红色。我可以删除:悬停在输入与jQuery的mouseover?我不想改变bg $('#container').css('backgroundColor','color'),我需要类似$('#container').removeAttr('hover')

回答

21

不幸的是,用jQuery管理CSS伪类是不可能的。

我建议你来操纵类,如下:

<style> 
    .red:hover{background:red} 
</style> 

<div id="container" class="red"><input type="submit"></div> 

<script> 
    $("#container").removeClass("red"); 
</script> 
+2

对不起,没有一个选项,我需要回#container解除状态 –

+1

但使用组合的问题是什么:'#container.red:hover {background:red; }'? – VisioN

4

无法删除伪类的规则,但您可以用事件绑定覆盖它们:

$("#container").on('mouseover', function() { 
    $(this).css('background', 'blue'); 
}).on('mouseout', function() { 
    $(this).css('background', whateverItIsNormally); 
}); 
2

这可能有点复杂,并且可以肯定使用优化,但是您可以使用迄今为止公布的组合:

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

我应用这个:

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

,它似乎在去除悬停性能工作,同时保留原来的CSS。