gpt4 book ai didi

javascript - 删除表中的空 td

转载 作者:行者123 更新时间:2023-11-30 19:38:45 25 4
gpt4 key购买 nike

我有一个表和 jquery 代码,我需要:如果 td 为空,则删除 tdtr 父级。

我该怎么做?

我的代码不起作用。在我的示例中,我有空的 td。我想删除 tr 父级。为什么我的代码不起作用?我究竟做错了什么?我为每个 td 搜索 td,如果 td 为空,我删除 tr 父级。但不工作..不是错误..

$('#table_serv tr').each(function(){ 
if( $(this).find('td').text().trim() === '' )
$(this).find('td').parent().remove(); // if it is empty, then remove parent tr
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="0" class="document-as" color="black" id="table_serv" style="font-size: 12px;border-right:0" width="100%">
<thead>
<tr>
<th style="text-align: center;color: white;font-weight: 600; width:5%"></th>
<th style="text-align: left;color: white;font-weight: 600;width:35%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Description</strong></span></span>
</th>
<th style="text-align: center;color: white;font-weight: 600;width:25%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Selling Price</strong></span></span>
</th>
<th style="text-align: center;color: white;font-weight: 600;width:15%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Quantity</strong></span></span>
</th>
<th style="text-align: center;color: white;font-weight: 600;width:15%"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Total</strong></span></span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">:0,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style=""></span><span class="de_de" style=""></span></td>
<td class="serv-price" style=""><span>0,00 €</span></td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;">0,00</span></td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>0,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">9:324,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style="">Justification safety (GOST-Norm)</span><span class="de_de" style="">Begründung Sicherheit (GOST-Norm)</span></td>
<td class="serv-price" style=""><span>324,00 €</span></td>
<td style="">1,00</td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>324,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">SER20:432,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style="">Certificate TR CU 004/020/2011 for 5 Years</span><span class="de_de" style="">Zertifikat TR CU 004/020/2011 für 5 Jahre</span></td>
<td class="serv-price" style=""><span>432,00 €</span></td>
<td style="">1,00</td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>432,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">7:34,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style="">Declaration TR ZU 04/2011 for 5 Years</span><span class="de_de" style="">Deklaration TR ZU 04/2011 for 5 Jahre</span></td>
<td class="serv-price" style=""><span>34,00 €</span></td>
<td style="">1,00</td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>34,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">:0,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style=""></span><span class="de_de" style=""></span></td>
<td class="serv-price" style=""><span>0,00 €</span></td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;">0,00</span></td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>0,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">9:23,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style="">Justification safety (GOST-Norm)</span><span class="de_de" style="">Begründung Sicherheit (GOST-Norm)</span></td>
<td class="serv-price" style=""><span>23,00 €</span></td>
<td style="">1,00</td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>23,00 €</strong></span></td>
</tr>
<tr>
<td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">SER20:43,00</span>
<br> &nbsp;</td>
<td style="text-align:left;"><span class="en_us" style="">Certificate TR CU 004/020/2011 for 5 Years</span><span class="de_de" style="">Zertifikat TR CU 004/020/2011 für 5 Jahre</span></td>
<td class="serv-price" style=""><span>43,00 €</span></td>
<td style="">1,00</td>
<td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>43,00 €</strong></span></td>
</tr>
<tr>
<td colspan="4" style=""><span>Sub Total</span></td>
<td><span>856,00 €</span></td>
</tr>
<tr>
<td colspan="4" style=""><span style="font-family:verdana,geneva,sans-serif;">Discount</span></td>
<td><span style="font-family:verdana,geneva,sans-serif;">0,00 €</span></td>
</tr>
<tr>
<td colspan="4" style=""><span style="font-family:verdana,geneva,sans-serif;">Total</span></td>
<td><span style="font-family:verdana,geneva,sans-serif;">856,00 €</span></td>
</tr>
<tr>
<td colspan="4" style=""><strong>&nbsp;&nbsp;&nbsp;&nbsp; Grand Total</strong></td>
<td style=""><span><span><strong>1343,92 €</strong></span></span>
</td>
</tr>
</tbody>
</table>

最佳答案

问题是

$(this).find('td').text()

将为您提供整行的单个文本,而不是单个 td。检查它在做什么很容易:

console.log($(this).find("td").text())

这将为第一行提供如下内容:

:0,00       0,00 €  0,00    0,00 €

看起来您正在尝试查找单个空单元格(而不是空行)- 因此您需要迭代各个 td

一种方法是:

$('#table_serv tr td').each(function() { 
if ($(this).text().trim() === '')
$(this).parent().remove();
});

关于javascript - 删除表中的空 td,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55646446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com