[英]How to append a class to table row by using parent function?
I want to append the class red-border
to a tr
element which contains a span
with class warning
.我想将类
red-border
附加到一个tr
元素,该元素包含一个带有类warning
的span
。 My code is not working.我的代码不起作用。 Please help.
请帮忙。
$(document).ready(function() { $("body").css("width", "2000px"); $(".jsgrid-table").find(".warning").parents().addClass("red-border"); });
.red-border { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="jsgrid-table"> <tbody> <tr class="jsgrid-row"> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;"><span class="warning"></span></td> </tr> </tbody> </table>
adding border
in the tr
element is not allowed.不允许在
tr
元素中添加border
。 Also, use .closest()
selector to find the parent that matches the conditions此外,使用
.closest()
选择器查找匹配条件的父级
$(document).ready(function() { $("body").css("width", "2000px"); $(".jsgrid-table .warning").closest("tr").addClass("red-border"); });
.red-border{ color:red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="jsgrid-table"> <tbody> <tr class="jsgrid-row"> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;"> <span class="warning"></span> </td> </tr> <tr class="jsgrid-row"> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> </td> </tr> </tbody> </table>
As in this snippet first tr
has class warning
and so class red-border
and in the next tr
children element of this tr
doesn't have warning
class and no red-border
is present in this tr
element.在这个片段中,第一个
tr
具有类warning
,因此类red-border
和在此tr
的下一个tr
子元素中没有warning
类,并且此tr
元素中不存在red-border
。
As said by @Thomas, .closest()
is working fine.正如@Thomas 所说,
.closest()
工作正常。 Check below the fiddle.检查小提琴下面。 As the borders are not supported for
tr
, I just have updated the color.由于
tr
不支持边框,我只是更新了颜色。
$(document).ready(function () { $("body").css("width", "2000px"); $(".jsgrid-table .warning").closest("tr").addClass("red-border"); });
.red-border{ color:red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="jsgrid-table"> <tbody> <tr class="jsgrid-row "> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> ... <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;"><span class="warning"></span></td> </tr> </tbody> </table>
<tr>
does not have a border
itself, but you can make it work: <tr>
本身没有border
,但您可以使其工作:
$("body").css("width", "2000px"); $(".jsgrid-table .warning").closest("tr").addClass("red-border");
tr.red-border { background: yellow; } tr.red-border > td { border-top: 2px dotted red; border-bottom: 2px dotted red; } tr.red-border > td:first-child { border-left: 2px dotted red; } tr.red-border > td:last-child { border-right: 2px dotted red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="jsgrid-table"> <tbody> <tr class="jsgrid-row"> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> <!--...--> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;"><span class="warning"></span></td> </tr> </tbody> </table>
You code is working fine except it adds the red-border on all parent elements.您的代码工作正常,只是它在所有父元素上添加了红色边框。 So, to add the class only on the tr tag, then pass the tr in the
.parents()
function.因此,要仅在 tr 标签上添加类,然后在
.parents()
函数中传递tr 。
$(".jsgrid-table").find(".warning").parents()
--- this selects all the parent elements of the element having class warning $(".jsgrid-table").find(".warning").parents()
--- 这将选择具有类警告的元素的所有父元素
$(".jsgrid-table").find(".warning").parents('tr')
--- this selects only tr element out of the all parent elements having class warning $(".jsgrid-table").find(".warning").parents('tr')
--- 这仅从所有具有类警告的父元素中选择tr元素
$(document).ready(function() { $("body").css("width", "2000px"); $(".jsgrid-table").find(".warning").parents('tr').addClass("red-border"); });
.red-border td{ color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="jsgrid-table"> <tbody> <tr class="jsgrid-row"> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;">2018</td> <td class="jsgrid-cell jsgrid-align-left" style="width: 100%;"><span class="warning"></span></td> </tr> </tbody> </table>
You can also check the below link for more details on parent, parents & colsest Difference between jQuery parent(), parents() and closest() functions您还可以查看下面的链接,在父母,父母和colsest更多的细节差异jQuery的父(),父母()和最近之间()函数
$(document).ready(function() {
$("body").css("width", "2000px");
$(".jsgrid-table").find(".warning").parents('tr').addClass("red-border");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.