Javascript 删除HTML中没有行的表
我有一个html页面,其中包含带有表标题的表。 如果加载文档时该表不包含任何行,则我希望在加载时删除该表。请注意,我不希望单击或执行任何其他操作Javascript 删除HTML中没有行的表,javascript,Javascript,我有一个html页面,其中包含带有表标题的表。 如果加载文档时该表不包含任何行,则我希望在加载时删除该表。请注意,我不希望单击或执行任何其他操作 <table class="Example:"> <th>Range from 12.12 to 1.12</th></table> <table class="Range:"> <th>Range from109.091to111.364</th> <tr&
<table class="Example:">
<th>Range from 12.12 to 1.12</th></table>
<table class="Range:">
<th>Range from109.091to111.364</th> <tr><td>f88c6441-3a2f-4c63-8b00-f63a75c09d57</td>
<td class="number">110.000</td></tr>
</table>
范围从12.12到1.12
范围从109.091到111.364 f88c6441-3a2f-4c63-8b00-f63a75c09d57
110
我想删除第一个表,保留第二个表。请注意,使用jquery时,此类表的数量可能是动态的:
<script>
$(document).ready(function(){
$('table').each(function(){
if($(this).find('td').length <= 0){
$(this).remove();
}
});
});
</script>
$(文档).ready(函数(){
$('table')。每个(函数(){
if($(this).find('td').length在th
之外缺少tr
元素
您可以获得表的行数,如果大于1(第一个是标题),则隐藏表
代码:
var rowCount=document.getElementsByClassName('Example')[0].rows.length;
如果(行数使用:
Javascript:
var table = document.getElementsByTagName("table");
for (var i = 0, a = table[i]; i < table.length; i++, a = table[i]) {
if (a.getElementsByTagName("td").length === 0) {
a.parentElement.removeChild(a);
}
}
var table=document.getElementsByTagName(“表”);
对于(变量i=0,a=table[i];i
演示:您没有在上面的表中添加任何行,a需要围绕。您到目前为止做了什么?显然,tr不需要围绕th进行包装这是从源代码复制的。表有一个标题,然后在第二种情况下有一行接受的答案使用jQuery,但没有jQuery标记…不,您需要使用不同的类$('.tableClass')/ids$('#tableID')或表索引$('table:eq(indexnumberthere)'))对于选择器。但这是动态生成的表。可以有任意数量的表。是否有方法检查具有给定类名的每个表并将其删除或隐藏?最后一件事。我正在尝试使用类名指定它,但它不起作用$('.classname')。每个…都是上述错误的
$("table:not(:has(td))").remove();
var table = document.getElementsByTagName("table");
for (var i = 0, a = table[i]; i < table.length; i++, a = table[i]) {
if (a.getElementsByTagName("td").length === 0) {
a.parentElement.removeChild(a);
}
}