gpt4 book ai didi

jquery - 单击复选框时更改唯一的行表颜色

转载 作者:行者123 更新时间:2023-12-01 08:32:20 24 4
gpt4 key购买 nike

我有一张这样的 table 。

    <table>
<tr class="rowA baris">
<td>A<td>
<Td><input class="checkbox" type="checkbox"><td>
</tr>
<tr class="rowB baris">
<td>B<td>
<Td><input class="checkbox" type="checkbox"><td>
</tr>
</table>

然后我想要这样的结果 Result

我希望选中复选框的行将 CSS 背景颜色更改为浅灰色。我该怎么做?

当前使用此 JQuery:

  $('.checkbox').click(function () {
if ($(this).is(":checked")) {

$('.baris').attr('style', 'background-color: lightgrey;');
}
});

但所有行都变成浅灰色。

最佳答案

您应该使用.closest()获得正确的行

$('.checkbox').click(function () {
var backgroundColor = $(this).is(":checked") ? "lightgrey;" : "";
$(this).closest('tr').attr('style', 'background-color: '+ backgroundColor +'');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr class="rowA baris">
<td>A<td>
<Td><input class="checkbox" type="checkbox"><td>
</tr>
<tr class="rowB baris">
<td>B<td>
<Td><input class="checkbox" type="checkbox"><td>
</tr>
</table>

关于jquery - 单击复选框时更改唯一的行表颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60087356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com