gpt4 book ai didi

javascript - 如何根据 tbody 的内容切换跨度的可见性

转载 作者:行者123 更新时间:2023-12-03 06:44:31 25 4
gpt4 key购买 nike

我正在使用表格来填充 tbody 中的数据,并且我还显示了一个跨度。数据是使用 Javascript 动态获取的。依赖关系是这样的:

如果tbody为空:显示SPAN

如果 tbody 有数据:隐藏 SPAN

//HTML 代码

<table id="incidents">
<tbody>
</tbody>
</table>
<br />
<button onclick="func()">Check table</button>
<br />

<span class="abc">Drop your files here to upload</span>

//Jquery代码

function func() {
var tbody = $("#incidents tbody");

if (tbody.children().length != 0) {
$('.abc').css('display','none');
}
}

最佳答案

使用trim作为额外检查

function func() {
var contentLen = $.trim($("#incidents tbody").children()).length;

if (contentLen == 0) {
$('.abc').css('display','none');
} else {
$('.abc').css('display','block');
}
}

关于javascript - 如何根据 tbody 的内容切换跨度的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37804197/

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