如何刷新/更新添加类到td:包含文本更改时

时间:2015-07-12 02:31:27

标签: jquery

我使用它将类添加到包含某些文本的td

$('td:contains("Setup")').addClass('mark');

在页面加载时,该类被添加到具有该文本的td单元格中,但是该页面上正在运行脚本,这将更改其中包含“是”的td,当发生这种情况时,我的选择器仍保留在老td并且不随新添加的那个移动。无论如何刷新我的选择器? jQuery的新手,所以任何帮助赞赏

2 个答案:

答案 0 :(得分:1)

快速,肮脏的方式只是手动.removeClass()就像使用.addClass()一样。

你可以使用从每个td元素中删除removeClass()的脚本。

    $('.portfolio_categories-mood-images').bind('hover', function(e){
    $('.portfolio_categories-mood-images').each(function(i){
    $(this).toggleClass('highlight-all');
    }, function() { $(this).removeClass('highlight-all'); }); });

然后再次检查哪个td需要它并使用您已经

的代码添加它
$("td").removeClass("mark");

答案 1 :(得分:1)

请阅读以下评论。什么事件触发了对td数据的更改?

$('td:contains("Setup")').addClass('mark');

您正在向active td添加一个类,其中包含您正在寻找的内容。您是否考虑从所有现有的TD中删除该类?

$('td').removeClass(); // This removes all classes. You could define 'mark' inside of the parenthesis if you wanted just that class gone.

所以剩下的代码是:

$('td').removeClass('mark');
$('td:contains("Setup")').addClass('mark');

这基本上是:

从所有现有mark 中删除课程td

将班级mark添加到包含Setup

的任何项目