如何使用jquery在表中选择一行? [英] How to select a row in table using jquery?

查看:98
本文介绍了如何使用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.

这篇关于如何使用jquery在表中选择一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆