我想在表格中隐藏空单元格.这是我的代码:
$(function() { $(".empty").each(hideCellIfEmpty); }); function hideCellIfEmpty() { var theCell = $(this); if (theCell.html().length == 0) { hideSoft(theCell); } } function hideSoft(jQElement) { jqElement.css('visibility','hidden'); }
table.empty { width: 350px; border-collapse: collapse; empty-cells: hide; } td.empty { border-style: solid; border-width: 1px; border-color: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <table class="empty"> <tr> <th></th> <th>Title one</th> <th>Title two</th> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty">value</td> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty"></td> </tr> </table>
你可以看到,第二行显示空单元格.但我想隐藏它.而且,我不想使用border-collapse:单独的.这是否可以使用border-collapse隐藏空单元格:崩溃?我也想知道为什么这会显示空单元格.
附:使用border-collapse:单独工作,不显示空单元格.
$(function() { $(".empty").each(hideCellIfEmpty); }); function hideCellIfEmpty() { var theCell = $(this); if (theCell.html().length == 0) { hideSoft(theCell); } } function hideSoft(jQElement) { jqElement.css('visibility','hidden'); }
table.empty { width: 350px; border-collapse: separate; empty-cells: hide; } td.empty { border-style: solid; border-width: 1px; border-color: blue; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <table class="empty"> <tr> <th></th> <th>Title one</th> <th>Title two</th> <th>Title three</th> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty">value</td> <td class="empty">value</td> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty"></td> <td class="empty">value</td> </tr> </table>
但这并没有回答这些问题:
解决方法
如果您的站点不需要IE 8及以下版本的支持,您可以使用CSS:empty伪类:
td:empty { visibility: hidden; }
table.empty { width: 350px; border-collapse: collapse; empty-cells: hide; } td.empty { border-style: solid; border-width: 1px; border-color: blue; } td:empty { visibility: hidden; }
<table class="empty"> <tr> <th></th> <th>Title one</th> <th>Title two</th> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty">value</td> </tr> <tr> <th>Row Title</th> <td class="empty">value</td> <td class="empty"></td> </tr> </table>
更多关于:空伪类可以在https://developer.mozilla.org/en-US/docs/Web/CSS/:empty找到