您要使用的not
filter,而不是:not
selector:
$('li').click(function() {
$(this).css('background-color', '#c7f1f2');
$('li').not(this).css('background-color', '');
});
演示:http://jsfiddle.net/ambiguous/ZUk88/
当你说li:not($(this))
然后你在尝试选择不是<$(this)>
元素的所有<li>
元素,这是没有意义的;同样为li:not(this)
。
你也可以从所有的列表项中删除背景颜色,然后把它添加到你想要的:
$('li').click(function() {
$('li').css('background-color', '');
$(this).css('background-color', '#c7f1f2');
});
演示:http://jsfiddle.net/ambiguous/L6ZNz/
而且,你对一下,但你的代码的问题举行会谈谈论悬停。
如果您已经连接到您的<li>
元素的颜色,你想,当有人点击另一个<li>
恢复这些颜色,那么你应该只添加和删除一个CSS类来改变颜色。 CSS的一点:
.hilight {
background-color: #c7f1f2 !important;
}
和一些jQuery的:
$('li').click(function() {
$(this).siblings('.hilight').removeClass('hilight');
$(this).addClass('hilight');
});
演示:http://jsfiddle.net/ambiguous/ZJpNa/
您使用 '悬停' 在这里的功能。你应该使用点击 –
@Jayantha如果他想让它在悬停时发生,则不需要。 –
是的,但他说,'当用户点击一行', –