【发布时间】:2011-10-13 18:00:54
【问题描述】:
我在页面上有一些按钮,它们的颜色通过 jQuery 更改,说明哪个按钮处于活动状态。我只想在悬停时添加颜色更改,然后在离开时返回原始颜色(由 jQuery 决定)。
我第一次使用 css:.showlink li:hover {color:#aaa;},它可以正常工作,除了当页面切换和 jQuery 更改颜色时,它会取代 CSS。
然后我决定使用简单的 jQuery,当有东西悬停时,改变它的颜色。这并不完全有效,因为它会永久改变颜色。为了缓解这种情况,我在函数中添加了一点,将其返回为不同的颜色。
有什么方法可以将它恢复为悬停时更改之前的原始颜色吗?
// Changes color on hover
$(function() {
$('.showlink').hover(function(){
$(this).css('color', '#aaa');
},
function(){
$(this).css('color', '#f3f3f3');
});
});
//Changes color depending on which page is active, fades that page in
$(function(){
$('#show_one').css('color', '#ffcb06');
$('#two, #three').hide();
});
$('.showlink').click(function(){
$('.showlink').css('color', '#f3f3f3');
$(this).css('color', '#ffcb06');
var toShow = this.id.substr(5);
$('div.page:visible').fadeOut(600, function(){
$('#' + toShow).fadeIn(600);
});
});
【问题讨论】:
-
如果没关系,直接杀掉style属性:$(this).removeAttr('style');
标签: javascript jquery css colors hover