[英]How can I change the background of a selected li in jquery?
我有一個帶有一堆列表項的無序列表。 我在單擊時設置所選<li>
的背景:
$(this).animate({
backgroundColor: '#0000FF'
}, 'fast');
當我單擊另一個<li>
,我想更改其backgroundColor
屬性,但我希望其余的<li>
都默認恢復為另一種顏色。 這樣,看來我正在更改所選狀態。
$('ul li').click(function() {
$(this).animate({backgroundColor:"#0000ff"},'fast')
.siblings().animate({backgroundColor:"#000000"},'fast'); // or whatever other color you want
});
我將使用類和CSS過渡的混合 :
$('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", "");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.