gpt4 book ai didi

javascript - 如何制作在表为空时隐藏表的 jquery 脚本?

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

我有一些 CSV 到 html 生成器。它生成 CSV 数据到 html 表。例如

 <table><tbody><tr><th>Name 1</th><th>Name 2</th></tr>
<tr><td>123</td><td>123</td></tr>
</tbody></table>

有时没有数据生成。然后表格看起来像这样(它只显示标题)

 <table><tbody><tr><th>Name 1</th><th>Name 2</th></tr>
</tbody></table>

在这种情况下,我想显示一些文本而不是表格。比方说:

 <h1>There is no data man!</h1>
<p>Contact to Us</p>

我该怎么做?

最佳答案

试试这个检查

   $('table').each(function(){
var element = $(this).find('tbody tr:nth-child(2)').length;
if (element == 0) {
$(this).empty();
$(this).append(" <h1>There is no data man!</h1><p>Contact to Us</p>")
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<table><tbody><tr><th>Name 1</th><th>Name 2</th></tr>

</tbody></table>

关于javascript - 如何制作在表为空时隐藏表的 jquery 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36450134/

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