2011-09-30 53 views
2

我有一大堆列表項的無序列表。我設置所選<li>的背景在其點擊:如何更改jquery中所選li的背景?

$(this).animate({ 
      backgroundColor: '#0000FF' 
}, 'fast'); 

當我點擊另一個<li>,我想改變它的backgroundColor屬性,但我想<li> S的其餘默認返回到另一種顏色。這樣,它看起來像我正在改變選定的狀態。

回答

5

你可以簡單地做到以下幾點:

$(this).siblings("li").css("backgroundColor", ""); 

Example on jsfiddle

0
$('ul li').click(function() { 
    $(this).animate({backgroundColor:"#0000ff"},'fast') 
     .siblings().animate({backgroundColor:"#000000"},'fast'); // or whatever other color you want 
}); 
0

我會用CSS轉換的mixe:

$('li').click(function(){) 
    $('li.active').removeClass('active'); 
    $(this).addClass('active'); 
}) 

li{background:#fff; -webkit-transition:all 0.25s linear; -moz-transition:all 0.25s linear;} 
li.active{background:#00f;} 
0
$('li').each.click(function(){ 
$(this).css("backgroundColor", ""); 

});