如何使用jQuery选择一行表

use*_*400 4 html css jquery html-table

我在我的应用程序上创建了一个表,代码相同

<table class="spreadsheet">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我通过以下jQuery代码单击选择一行

 $("tr").click(function(){
 $(this).toggleClass("otherstyle");
}
Run Code Online (Sandbox Code Playgroud)

我的css是

tr.otherstyle td
{
    background-color: #d1db3e;
    color:#000;
}
Run Code Online (Sandbox Code Playgroud)

我希望当我点击一行时,不选择其他行,只选择一行.

我们怎么能创造这个?

rah*_*hul 8

$("table.spreadsheet tr").click(function(){
    $("table.spreadsheet tr").removeClass("otherstyle");
    $(this).toggleClass("otherstyle");
});
Run Code Online (Sandbox Code Playgroud)

查看工作演示


Rei*_*gel 5

$("tr").click(function(){

 $('tr').removeClass("otherstyle");
 $(this).toggleClass("otherstyle");

}
Run Code Online (Sandbox Code Playgroud)

要么

$("tr").click(function(){     

 $(this).toggleClass("otherstyle").siblings().removeClass("otherstyle");

}
Run Code Online (Sandbox Code Playgroud)