Javascript 带拆分的jQuery多选择器

Javascript 带拆分的jQuery多选择器,javascript,jquery,jquery-selectors,split,Javascript,Jquery,Jquery Selectors,Split,我在这一行有两个,它们的类是messageROW和titleROW,后面有一个ID为“activecd”的复选框。当复选框被切换时,我试图更改消息行和标题行(整行)的背景颜色,但标题行只更改颜色 有什么建议吗 $('[id^=activecd]').change(function(){ $('.messageROW,.titleROW'+$(this).prop('id').split('activecd')[1]).closest('td').toggleClass('colorcod

我在这一行有两个
,它们的类是messageROWtitleROW,后面有一个ID为“activecd”的复选框。当复选框被切换时,我试图更改消息行标题行(整行)的背景颜色,但标题行只更改颜色

有什么建议吗

$('[id^=activecd]').change(function(){
    $('.messageROW,.titleROW'+$(this).prop('id').split('activecd')[1]).closest('td').toggleClass('colorcode', this);
});
HTML



我建议如下,尽管未经测试:

$('[id^=activecd]').change(function(){
    $(this).closest('tr').toggleClass('colorcode', this.checked);
});
这将侦听指定元素上的
change
事件,查找最近的
tr
元素,如果选中复选框,则添加
colorcode
类,如果未选中,则删除该类。

请尝试jQuery
$()。添加(选择器)
$()。使用css切换类(“bgOn”)

<style>
.bgOn {background:rgb(255,230,230)} // any css to toggle.
</style>

.bgOn{background:rgb(255230230)}//任何要切换的css。
我想这个问题不太清楚

<button onclick="someFunction(this)">Toggle color</button>

function someFunction(this){
  var $this=$(this);
  $this.parent().find(".messageROW,.titleROW").toggleClass("bgOn");
  // or
  $this.parent().parent().find("tr>td:first-child, tr>td:nth-child(2)").toggleClass("bgOn");
}
切换颜色
函数someFunction(this){
var$this=$(this);
$this.parent().find(“.messageROW,.titleROW”).toggleClass(“bgOn”);
//或
$this.parent().parent().find(“tr>td:first child,tr>td:nth child(2)”).toggleClass(“bgOn”);
}

可能会对您有所帮助。

如果您试图更改“整行”,那么只需使用
$(this).nest('tr')。toggleClass('colorcode')
我在HTML中添加了。。。没什么特别的。我试过使用.nestest('tr'),但没有效果。虽然它对td有效…但对'tr'不起作用,但对'td'(包含复选框的单元格)有效。真的吗?这意味着您需要修改css以适当地设置元素的样式,例如:
tr.colorcode,tr.colorcode td{/*styles here*/}
要查看它是否工作,请检查DOM,没有理由不工作(因为类应该添加到
tr
)。将我的css从“.colorcode”更改为“tr.colorcode td”修好了。非常感谢。
<button onclick="someFunction(this)">Toggle color</button>

function someFunction(this){
  var $this=$(this);
  $this.parent().find(".messageROW,.titleROW").toggleClass("bgOn");
  // or
  $this.parent().parent().find("tr>td:first-child, tr>td:nth-child(2)").toggleClass("bgOn");
}