简体   繁体   English

带有拆分的jQuery多重选择器

[英]jQuery Multiple Selector with split

I have this row with two <td> 's with classes messageROW and titleROW following a checkbox with the ID of ' activecd '. 我有一个带有两个<td>的行,其类别为messageROWtitleROW,其后为ID为' activecd '的复选框。 I'm trying to change the bg color of both messageROW and titleROW (the whole row) when the checkbox is toggled but instead titleROW only changes colors. 我试图在切换复选框时更改messageROWtitleROW (整个行)的bg颜色,但titleROW仅更改颜色。

Any suggestions? 有什么建议么?

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

HTML : HTML

 <tr> <td class="messageROW"></td> <td class="titleROW"></td> <td><input id="activecd"></td> </tr> 

I'd suggest the following, albeit untested: 我建议以下内容,尽管未经测试:

$('[id^=activecd]').change(function(){
    $(this).closest('tr').toggleClass('colorcode', this.checked);
});

This listens for the change event on the specified element(s), finds the closest tr element and adds the colorcode class if the checkbox is checked, and removes the class if not. 这将侦听指定元素上的change事件,找到最接近的tr元素,如果选中此复选框,则添加colorcode类,如果未选中,则删除该类。

Try jQuery $().add(selector) , $().toggleClass("bgOn") with css 尝试使用CSS $().add(selector) jQuery $().add(selector)$().toggleClass("bgOn")

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

Not clear question, I think. 我认为问题尚不明确。

<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");
}

might help you. 可能会帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM