admin管理员组文章数量:1404923
How can I add class to second td if the first one contains word ZERO?
$('td:nth-child(1):contains("ZERO")').closest('td:nth-child(2)').addClass('intro').removeClass('part-pakiet');
How can I add class to second td if the first one contains word ZERO?
$('td:nth-child(1):contains("ZERO")').closest('td:nth-child(2)').addClass('intro').removeClass('part-pakiet');
Share
Improve this question
asked Oct 27, 2016 at 10:42
gainskygainsky
17510 bronze badges
1
-
have you
.next()
. – Rajesh Commented Oct 27, 2016 at 10:44
4 Answers
Reset to default 7Use an adjacent sibling binator (+
):
$("td:nth-child(1):contains(ZERO) + td").addClass('intro').removeClass('part-pakiet');
Example:
$("td:nth-child(1):contains(ZERO) + td").addClass('intro').removeClass('part-pakiet');
body {
font-family: sans-serif;
}
table {
border-collapse: collapse;
}
td, td {
border: 1px solid #ddd;
}
.intro {
color: green;
font-weight: bold;
}
<table>
<tbody>
<tr>
<td>first cell - no match</td>
<td>second cell</td>
<td>third cell with ZERO</td>
<td>...to prove to ourselves this doesn't match</td>
</tr>
<tr>
<td>first cell - ZERO</td>
<td>second cell</td>
<td>third cell with ZERO</td>
<td>...to prove to ourselves this doesn't match</td>
</tr>
<tr>
<td>first cell - no match</td>
<td>second cell</td>
<td>third cell with ZERO</td>
<td>...to prove to ourselves this doesn't match</td>
</tr>
<tr>
<td>first cell - ZERO</td>
<td>second cell</td>
<td>third cell with ZERO</td>
<td>...to prove to ourselves this doesn't match</td>
</tr>
</tbody>
</table>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
In your sample, you use closest
incorrectly. Closest here refers to the closest ancestor element matching the selector (or the current element if it matches the selector). So in this case you have to get the closest tr
, and then its second td
. For the record, here is a working code using closest
.
$('td:nth-child(1):contains("ZERO")').closest('tr').find('td:nth-child(2)').addClass('intro').removeClass('part-pakiet');
However, in this scenario you don't need to use closest
, because there is a simpler way using next
selector.
$('td:nth-child(1):contains("ZERO")').next().addClass('intro').removeClass('part-pakiet');
More literal approach if the text is definitely equal to that word. Other wise use the :contains pseudo:
var tableElement = $('.table td:first-child'),
wordToCheck = 'ZERO';
if (tableElement.text() === wordToCheck) {
tableElement.next().addClass('intro').removeClass('part-pakiet');
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tbody>
<tr>
<td>ZERO</td>
<td class="part-pakiet"></td>
</tr>
</tbody>
</table>
You can use the filter()
function for this:
$('td').filter(function(){
return ~$(this).prev('td').text().indexOf('ZERO'); // check if prev td contains 'ZERO' text
}).addClass('intro').removeClass('part-pakiet');
本文标签: javascriptHow can I add class to second td if the first one contains some wordStack Overflow
版权声明:本文标题:javascript - How can I add class to second td if the first one contains some word? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744877936a2630028.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论