如何使用jquery在表中选择一行?

问题描述:

我在我的应用程序中创建了一个表,我想在单击复选框时选择(更改背景颜色)整行,与gmail一样,当我们在gmail中单击复选框时,整行变为黄色。

I have created a table in my application, I want to select (change background color) whole row on click of a checkbox, same as gmail is doing, When we click checkbox in gmail, the whole row becomes yellow.

<table>
<tbody>
<tr>
<td><input type="checkbox" name="chk" id="chk" /></td>
<td>My Name</td>
<td>Description of the job</td>
</tr>
</tbody>
</table>

请告诉我如何在jquery中做同样的事情?

Please tell me how to do the same in jquery?

$(function() {
  $("#chk").click(function() {
    $(this).parents("tr").toggleClass("diffColor");
  });
});

创建一个CSS类(上面称为diffColor)并添加背景颜色,类似于:

Create a CSS class (called "diffColor" above) and add the background color that way, something like:

<style type="text/css">
tr.diffColor td { background-color: yellow; }
</style>

不要直接设置CSS属性。尽可能使用课程。

Don't set CSS attributes directly. Use classes where possible.