首页 > 解决方案 > 如果“活动”类在前面,则将“活动”类添加到 TD

问题描述

我不怎么做 jQuery,所以这对其他人来说可能很容易。我有一系列的桌子。有些需要有一个特定的列才能激活。我现在的问题显然是所有表格下面的代码都突出显示了特定$indexNum值并添加了活动类。

我需要说。th“您是否在其中一个标签上找到了活动类thead?”

是的,比在此特定表中查找所有 td 并将活动类应用于与您在 thead 中找到的列相同的列。

不,那么不要对这张桌子做任何事情。

这是我到目前为止所拥有的:

var $indexNum = $('thead tr th.active').index();
$('td:nth-child('+($indexNum+1)+')').addClass('active');

标签: jquerycss-selectors

解决方案


我认为这样的事情可以完成这项工作:

$('table th.active').each(function () {
  var index = $(this).index()
  $(this).parents('table').find('td:nth-child(' + (index + 1) + ')').addClass('active')
})

推荐阅读