我有一张 table :
<table>
<thead>
<tr>
<th></th>
...
<tr>
...
</thead>
<tbody>
<tr>
<td></td>
...
</tr>
<tr>
<td></td>
...
</tr>
<tr> I click here but I want that class refers to the previous row
<td></td>
...
</tr>
</tbody>
</table>
我使用 jQuery 向单击的行添加了一个类。我希望那个类指的是被点击的前一行。
我想我应该使用第 nth-child..?
最佳答案
您可以使用 jQuery .prev()实现这一目标的功能。
你会像这样使用它:
HTML
<table border="1">
<thead>
<tr>
<th>Head</th>
<tr>
</thead>
<tbody>
<tr>
<td>Data</td>
</tr>
<tr>
<td>Data</td>
</tr>
<tr>
<td> I click here but I want that class refers to the previous row</td>
</tr>
</tbody>
</table>
jQuery
$(document).ready(function(){
$('tr').click(function(){
$(this).prev().css({"background-color":"pink"});
});
});
希望这对您有所帮助!
关于jquery - 使用 CSS 和 jQuery 检测所选行的前一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34902094/