1

可能重复:
Jquery:单击时突出显示/取消突出显示表行

我创建了一个包含几个 TR 标签的 html 表。现在我想要,当用户点击表格中的一行时,它会突出显示,而之前点击的则不突出显示。谁能告诉我该怎么做。我正在尝试使用 jquery 来做到这一点。

4

2 回答 2

0

尝试这个:

// on click of any of the table cell...
$("td").click(function() {

    // Remove the active class from all the tr's
    $(this).closest("tr").siblings().removeClass("active");

    // Add the active class to the clicked table row...
    $(this).parents("tr").toggleClass("active", this.clicked);
});

CSS

这里没什么可看的,只是我们将准备好一个类名来处理“当前”行和列的实际样式。

.active { background-color: #eee; }

在这里提琴

于 2012-12-22T07:34:31.147 回答
0

这是一个简单的解决方案。它将类添加/删除hilite到最新的 clicked <tr>

$( 'tr' ).on( 'click', function() {
    var classname = 'hilite';
    $( 'tr.' + classname ).add( this ).toggleClass( classname );
} );

你可以在这里测试它:http: //jsfiddle.net/GvXKe/

于 2012-12-22T07:35:21.193 回答