如果它是空的,我想洗掉整個列。
這段代碼是我發現的唯一有效的,只是除了最后一列之外,每一列都會被洗掉,即使它是空的。我不知道為什么。非常感謝任何幫助。
這就是我的頁面的外觀:
/* loop over each th */
$('th').each(function(idx, el) {
/* check every td in the same column, see if they contain any text */
var check = !!$('tr').find('td:eq(' idx ')').filter(function() {
return $.trim($(this).text()).length;
}).length;
/* toggle the display of each th and td in this column, based on the check above */
$('tr').find('td:eq(' idx '), th:eq(' idx ')').toggle(check);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
uj5u.com熱心網友回復:
它似乎作業得很好——也許是你的 HTML
我添加了 tbody 和 thead
/* loop over each th */
$('th').each(function(idx, el) {
/* check every td in the same column, see if they contain any text */
var check = !!$('tbody tr').find('td:eq(' idx ')').filter(function() {
return $.trim($(this).text()).length;
}).length;
/* toggle the display of each th and td in this column, based on the check above */
$('tr').find('td:eq(' idx '), th:eq(' idx ')').toggle(check);
});
tr, td { border: 1px solid black; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td></td>
<td>3</td>
</tr>
</tbody>
</table>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507816.html
標籤:javascript html jQuery