[英]How to hide tr items when td is 0 in jquery?
我想隐藏td
文本为0的所有<tr>
。我该怎么办? 我不得不提到,实际上我有600多行。 但是下面的示例是一个演示。 谢谢
<table id ="list2">
<tbody>
<tr>
<td>2</td>
<td>213</td>
<td id ="hideRow">0</td>
<tr>
<tr>
<td>vb</td>
<td>asf</td>
<td id ="hideRow">0</td>
<tr>
<tr>
<td>cxvb</td>
<td>xcvb</td>
<td id ="hideRow">2</td>
<tr>
<tr>
<td>cas</td>
<td>asdf</td>
<td id ="hideRow">45</td>
<tr>
</tbody>
</table>
这是我的尝试:| 。 该事件由onclick事件加载
$('#list2').find("tr td #hideRow").each(function(){
var txt2 = $(this).text();
if (txt2 =="0"){
$('#list2').find("tr").each(function(){
$(this).hide();
});
}
})
首先,请勿将id用作重复的名称。 尝试按照以下步骤进行操作。
<table id ="list2">
<tbody>
<tr>
<td>2</td>
<td>213</td>
<td class="hideRow">0</td>
<tr>
<tr>
<td>vb</td>
<td>asf</td>
<td class="hideRow">0</td>
<tr>
<tr>
<td>cxvb</td>
<td>xcvb</td>
<td class="hideRow">2</td>
<tr>
<tr>
<td>cas</td>
<td>asdf</td>
<td class="hideRow">45</td>
<tr>
</tbody>
</table>
$('#list2').find(".hideRow").each(function(){
var txt2 = $(this).text();
if (txt2 =="0"){
$(this).parent().hide();
}
})
元素上的ID必须是唯一的,您不能有多个<td id="hideRow">
元素,并且希望所有时间都能正常播放。 我建议将其更改为一堂课。 然后,选择所有元素:
var elems = $('span.hideRow');
过滤到文本为0
那些:
elems = elems.filter(function() {
return $(this).text() === "0";
});
获取其父<tr>
元素:
elems = elems.closest('tr');
然后,最后将它们隐藏起来:
elems.hide();
显然,所有这些都可以一行完成:
$('span.hideRow').filter(function() {return $(this).text() === "0";}).closest('tr').hide();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.