我是jQuery新手。我尝试了以下2个脚本,但它们不起作用。我验证了的内容att1_preview_lbl.Text
为空并且相同att1_preview
,但该行没有隐藏或被删除。有人可以帮忙吗?
JavaScript:
<script>
$('.EventDetail tr').filter(function() {
return $(this).find('td').filter(function() {
return !$.trim($(this).text());
}).length;
}).hide();
</script>
<script>
$("table tr").each(function() {
var cell = $.trim($(this).find('td').text());
if (cell.length == 0) {
console.log('empty');
$(this).addClass('nodisplay');
}
});
</script>
HTML:
<table cellspacing="0" cellpadding="0" width="100%" border="0" style="border-style: groove">
<tbody>
<tr>
<td class="PaddedRight" style="border-style: groove">
<asp:Label ID="att1_preview_lbl" runat="server">Attribute 1</asp:Label>
</td>
<td style="border-style: groove">
<asp:Label ID="att1_preview" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="PaddedRight" style="border-style: groove">
<asp:Label ID="att2_preview_lbl" runat="server">Attribute 2</asp:Label>
</td>
<td style="border-style: groove">
<asp:Label ID="att2_preview" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="PaddedRight" style="border-style: groove">
<asp:Label ID="att3_preview_lbl" runat="server">Attribute 3</asp:Label>
</td>
<td style="border-style: groove">
<asp:Label ID="att3_preview" runat="server"></asp:Label>
</td>
</tr>
<tr>
</tbody>
</table>
好吧-我在几次错误的开始之后就明白了。遍历每一行的单元格,hasData
如果该行中的任何单元格都有数据,则将其设置为true。单元格迭代完成后,如果hasData
仍然为false ,则隐藏该行。
//loop through the rows
$("table tr").each(function (i, row) {
//reset hasData to false for each row
var hasData = false;
//loop through the cells of each row
$(row).children().each(function (i, cell) {
//If a cell has data, set hasData to true
if ($(cell).text().trim().length != 0) {
hasData = true;
}
});
//Hide the row if hasData is still false after iterating through the cells
if (hasData == false) {
$(this).hide();
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句