我有像这样的HTML代码。
<h4 id="dd">Resource usage</h4>
<table border="1" class="dataframe table table-striped table-bordered table-hover">
<thead>
<tr style="text-align: right;">
<th>peak_value</th>
<th>resource_name</th>
</tr>
</thead>
<tbody>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
</tbody>
</table>
我从数据库中获取表。因此,数据是动态创建的。 现在我不想显示表,如果它的整行只有“NA”值(即),使用javascript值NA。 请帮帮我
答案 0 :(得分:1)
您可以迭代td
元素并查看是否有任何元素具有非NA
值,如果不隐藏表格
var table = document.querySelector('#dd + table');
var toShow = Array.from(table.querySelectorAll('td')).some(td => td.innerHTML.trim() != 'NA');;
if (!toShow) {
table.style.display = 'none';
}
<h4 id="dd">Resource usage</h4>
<table border="1" class="dataframe table table-striped table-bordered table-hover">
<thead>
<tr style="text-align: right;">
<th>peak_value</th>
<th>resource_name</th>
</tr>
</thead>
<tbody>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
</tbody>
</table>
演示:Fiddle
如果你想处理行
var table = document.querySelector('#dd + table');
var tableHasNonNAValue = false;
Array.from(table.querySelectorAll('tbody tr')).forEach(tr => {
var hasNonNAValue = Array.from(tr.querySelectorAll('td')).some(td => td.innerHTML.trim() != 'NA');
tableHasNonNAValue = tableHasNonNAValue || hasNonNAValue;
if (!hasNonNAValue) {
tr.style.display = 'none';
}
});
if (!tableHasNonNAValue) {
table.style.display = 'none';
}
<h4 id="dd">Resource usage</h4>
<table border="1" class="dataframe table table-striped table-bordered table-hover">
<thead>
<tr style="text-align: right;">
<th>peak_value</th>
<th>resource_name</th>
</tr>
</thead>
<tbody>
<tr>
<td>NA1</td>
<td>NA</td>
</tr>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
</tbody>
</table>
演示:Fiddle
答案 1 :(得分:0)
jQuery.filter
过滤所有td
元素,文字为"NA"
td
元素中的tr
元素的长度检查已过滤集合的长度tr
有td
个元素且长度相等,hide()
tr
元素。
$('tr').each(function() {
var tdElems = $(this).find('td');
var filtered = tdElems.filter(function() {
return this.textContent.trim() === 'NA';
});
if (tdElems.length && (filtered.length === tdElems.length)) {
$(this).hide();
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h4 id="dd">Resource usage</h4>
<table border="1" class="dataframe table table-striped table-bordered table-hover">
<thead>
<tr style="text-align: right;">
<th>peak_value</th>
<th>resource_name</th>
</tr>
</thead>
<tbody>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
<tr>
<td>NA</td>
<td>NA</td>
</tr>
</tbody>
</table>