更新:这里是一个更好的版本使用CSS类:
$(".simplehighlight").click(function() {
$(this).toggleClass('clicked');
// comment the following line if you don't want to dehighlight other rows
$(this).siblings().removeClass('clicked');
});
$(".simplehighlight").hover(function() {
$(this).children().css('backgroundColor', '#ffdc87');
}, function() {
$(this).children().css('backgroundColor', '');
});
凡CSS是:
.simplehighlight {
background-color: #fff;
}
.clicked {
background-color: #ffdc87;
}
DEMO
老答案:作品,但不必要的复杂。
click
只接受一个回调,而不是两个。你可以使用data
检查行是否被点击与否:
$(".simplehighlight").click(function() {
var clicked = $(this).data('clicked');
$(this).data('clicked', !clicked);
if(clicked) {
$(this).children().css('backgroundColor', '#fff');
}
else {
$(this).children().css('backgroundColor', '#ffdc87');
}
});
然后以不变化对mouseleave
来电,检查clicked
值:
$(".simplehighlight").hover(function() {
$(this).children().css('backgroundColor', '#ffdc87');
},
function() {
if(!$(this).data('clicked')) {
$(this).children().css('backgroundColor', '#fff');
}
});
Working DEMO
会发生什么事,如果你点击另一行?先前点击的行是否应该突出显示? – 2010-12-17 16:47:25
没有。当前点击的行应该突出显示,并且即使将鼠标悬停在其他行上,当前行也应该高亮显示。 – DG3 2010-12-17 16:50:03