2012-12-05 59 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。