我有一大堆列表項的無序列表。我設置所選<li>
的背景在其點擊:如何更改jquery中所選li的背景?
$(this).animate({
backgroundColor: '#0000FF'
}, 'fast');
當我點擊另一個<li>
,我想改變它的backgroundColor
屬性,但我想<li>
S的其餘默認返回到另一種顏色。這樣,它看起來像我正在改變選定的狀態。
我有一大堆列表項的無序列表。我設置所選<li>
的背景在其點擊:如何更改jquery中所選li的背景?
$(this).animate({
backgroundColor: '#0000FF'
}, 'fast');
當我點擊另一個<li>
,我想改變它的backgroundColor
屬性,但我想<li>
S的其餘默認返回到另一種顏色。這樣,它看起來像我正在改變選定的狀態。
$('ul li').click(function() {
$(this).animate({backgroundColor:"#0000ff"},'fast')
.siblings().animate({backgroundColor:"#000000"},'fast'); // or whatever other color you want
});
我會用類和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;}
$('li').each.click(function(){
$(this).css("backgroundColor", "");
});