我正在尝试将类添加到整个row
,其中text为“null”或者rating小于或等于5.
$( document ).ready(function() {
$("thead tr td:nth-child(3)").each(function(){
if($(this).text() == 'null')
$(this).text('No rating found').addClass("table-danger");
else if ( $(this).text() <= '5' ) {
//if value is smaller or equal to 5 don't works
$(this).text().addClass("table-danger");
}
});
});
FiddleJS :https://jsfiddle.net/e0svzss9/6/
答案 0 :(得分:2)
此行错误
$(this).text().addClass("table-danger");
应该是
$(this).addClass("table-danger");
编辑:
使用Parents标签导航到tr
即
$(this).parents('tr')
答案 1 :(得分:1)
您可以使用父('tr')来处理您的代码。
$(this).text('No rating found').parent('tr').addClass("table-danger");
答案 2 :(得分:0)
而不是
$(this).text().addClass("table-danger");
添加
$(this).parents('tr').addClass("table-danger");
答案 3 :(得分:0)
您可以使用parent()
$(this).text('No rating found').parent().addClass("table-danger");