单击“将类添加到第一个”<td>在行中并且对应<th>表中
On Click add Class to First <td> in Row and corresponding <th> in Table
我有一个表,它有可点击的<td>
。
当我点击表中的单元格时,我想向添加一个类
- 我单击的行中的第一个
<td>
- 对应于行所在列的
<th>
从本质上讲,我试图直观地显示哪个<td>
被点击了。
我可以想出如何将类添加到行中的第一个<td>
,但它会将它们全部添加到其中,而不仅仅是我所在的那个。我无法想出如何将其添加到<th>
正在查找有关如何在Jquery中执行此操作的一些建议。我不完整的代码是:
//Adding Class Needed
$('.table-bordered tr td').on('click',function(){
//Add Class to First TD in ROW
$("tr td:nth-child(1)").addClass('superStyle');
//Add Class to Header <th> Cell above
});
演示http://jsfiddle.net/L8s5X/
尝试
$(function(){
//Hover and click colors
var table = $('table').on('click', 'td', function(){
$('.table-bordered tr td').removeClass('active');
$(this).addClass('active');
});
//Adding Class Needed
$(table).find('tr td').on('click',function(){
//Add Class to First TD in ROW
table.find('.superStyle').removeClass('superStyle');
$(this).closest('tr').find("td:nth-child(1)").addClass('superStyle');
//Add Class to Header <th> Cell above
table.find('thead th').eq($(this).index()).addClass('superStyle')
});
});
演示:Fiddle
当然是这样,您的选择器会被告知这样做。
您需要在点击的上下文中创建选择器
$(this).parent().find('td:nth-child(1)').addClass('superStyle');
演示:http://jsfiddle.net/L8s5X/7/
演示
$('.table-bordered tr td').on('click', function () {
var $table = $(this).closest('table'),
$tds = $(this).closest('tr').find('td');
$table.find('.superStyle').removeClass('superStyle');
//Add Class to First TD in ROW
$tds.eq(0).addClass('superStyle');
//Add Class to Header <th> Cell above
var index = $tds.index(this);
$table.find('th').eq(index).addClass('superStyle');
});
此解决方案似乎可以按需工作:
jsfiddle
// save elements before binding the event handler, to save processing time
var $table = $('table'),
$headings = $table.find('thead th'),
$fields = $table.find('td');
$table.on('click', 'td', function () {
var $this = $(this),
index = $this.index();
$fields.removeClass('active');
$this.parent().children(':first-child').addClass('active');
$headings.removeClass('superStyle').eq(index).addClass('superStyle');
});
相关文章:
- 在<页眉>标签
- 如何更改<svg>标记为<img>用js标记
- 如何逃离<>在javascript下划线模板中
- 有没有一种方法可以检测ios<>使用jquery和触发器操作形成导航按钮
- 正则表达式,它允许除“”之外的所有字符<>_;{}[]”;
- ajax调用:响应<->数据类型
- 如果所有三个文本框<>然后是100%
- CORS的问题.烧瓶<->AngularJS
- 有没有一种方法可以创建与Java<->JSON映射对象,如JSON.stringfy(jsObject)创建的
- 什么是%<>%调用了语法
- JavaScript返回值:What dos<>意思是
- JavaScript中哪种类型的值对应于Java`List<>`
- 如何发送类似“<>"在参数中使用$.ajax-to-server
- 不等于<>操作员不工作
- jquery附加函数转换<>到实体名称
- 只有当超过3个元素时才显示更多内容按钮,否则不显示.使用jquery's:lt:gt
- 为什么在<>ckeditor中不支持
- url有效性的Java脚本regexp,带有<>
- 设计Javascript前端<->C++后端通信
- 比较查询运算符ObjectId<->日期