【问题标题】:Hide empty cells in table隐藏表格中的空单元格
【发布时间】:2012-08-14 23:07:20
【问题描述】:

我想隐藏表格中的空单元格。这是我的代码:

$(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:separate。这可以使用border-collapse:collapse 隐藏空单元格吗?我也想知道为什么显示空单元格。

附:使用 border-collapse: separate 可以正常工作,并且不会显示空单元格。

$(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>

但这并不能回答这些问题:

  1. 为什么使用border-collapse: collapse时会显示空单元格?

  2. 为什么使用border-collapse: separate时不显示空单元格?

【问题讨论】:

    标签: html-table hide cells


    【解决方案1】:

    试试the following

    <style type="text/css">
    table.empty{
        width:350px;
        border-collapse: collapse;
        empty-cells:hide;
    }
    td.normal{
        border-style:solid;
        border-width:1px;
        border-color: blue;
    }   
    td.empty{      
        style:'display=none'
    }
    

    </style>
    <table >
    <tr>
    <th></th>
    <th>Title one</th>
    <th>Title two</th>
    </tr>
    <tr>
    <th>Row Title</th>
    <td class="normal">value</td>
    <td class="normal">value</td>
    </tr>
    <tr>
    <th>Row Title</th>
    <td class="normal">value</td>
    <td class="empty"></td>
    </tr>
    </table>​
    

    【讨论】:

    • 非常感谢。但是我们没有使用空单元格属性来隐藏空单元格。假设我们正在使用 java 脚本填充表格内容,那么我们不知道哪个单元格将是空的。在这种情况下,上述解决方案将不起作用。所以我想使用空单元格属性。我想知道空单元格显示为border-collapse的原因:折叠?
    • 不客气!在填写表格时(使用 JS),您也可以更改 td 类。如果您在代码中的两个不同位置执行此操作(您不应该这样做),您仍然可以使用 JS 检查单元格内的值并分别更改 td 的类。关于border-collapse,它只是将边框设置为单行。
    • 这是一个很好的答案。我对您为我的问题提供的替代解决方案几乎感到满意。我仍在寻找使用空单元格属性的解决方案。顺便说一句,非常感谢。
    • 不使用 JS/jQuery 将无法工作,因为td 的属性会覆盖table 的属性,因此会显示边框。
    • 更改边框:折叠为边框:分离并查看隐藏的空单元格。检查这个jsfiddle.net/blunderboy/gVWQE/4
    【解决方案2】:

    假设你想要隐藏的所有单元格都有类 '.empty()' 我想出了这段 jQuery:

    $(function(){
        $(".empty").each(hideCellIfEmpty);
    });
    
    function hideCellIfEmpty(){
        var theCell = $(this);
        if(theCell.html().length == 0){
            theCell.hide();
        }           
    }​
    

    aaaaand...it seems to work。 :)

    但是,如果您尝试制作甜甜圈形状,hide() 不会保留您遇到this problem 的空间。
    幸运的是,is another question 讨论了这个问题,答案是使用

    css('visibility','hidden')
    

    你也可以在this fiddle找到女巫。

    【讨论】:

    • 非常感谢您分享的精彩概念。更新了问题。
    • 很高兴你喜欢它 :) 至于 CSS,我对这件事不太有信心贡献一些有意义的东西,所以让我们希望一些聪明的 CSS 大师能找到你的问题 ^^
    【解决方案3】:

    我在正在阅读的一篇好文章中找到了this 解决方案。

    我希望它也对你有用:

    table {
       empty-cells: hide;
    }
    

    最好的问候!

    【讨论】:

      【解决方案4】:

      如果您的网站不需要支持 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>

      更多关于:empty伪类的信息可以在https://developer.mozilla.org/en-US/docs/Web/CSS/:empty找到

      【讨论】:

      • 谢谢@Peter。这是你提到的新事物。我接受
      • 感谢您的解决方案,效果很好!旁注:通常,标题单元格标记为 th 而不是 td - 至少在我的情况下,将 td 更改为 th 看起来更好。
      • 就我而言,display: none;visibility 工作得更好
      【解决方案5】:

      这是另一个解决方案,因为 td:empty 对我不起作用:

      <style type="text/css">
          table {
              border-spacing: 0px; // removes spaces between empty cells
              border: 1px solid black;
          }
          tr, td {
              border-style: none; // see note below
              padding: 0px; // removes spaces between empty cells
              line-height: 2em; // give the text some space inside its cell
              height: 0px; // set the size of empty cells to 0
          }
      </style>
      

      给定的代码将完全删除空行会占用的任何空间。 不幸的是,您必须设置border-style: none;,否则空单元格的边框无论如何都会被绘制(这会导致粗线)。

      【讨论】:

        【解决方案6】:

        刚刚使用的 CSS:empty-cells: hide;

        浏览器支持Verified LinkLink 2

        table { 
            border-collapse: separate;
            empty-cells: hide;
        }
        

        注意:您不能使用border-collapse: collapse;,因为它会使禁用 看起来空单元格隐藏

        /******Call-Padding**********/
        
        table { 
          /***
          border-collapse: collapse; #Not use it     ***/
            border-collapse: separate;
            empty-cells: hide;
        }
        
        td { 
          border: 1px solid red;
          padding: 10px;
        }
        	<table>
        		<tr>
        			<th>Head1 </th>
        			<th>Head2 </th>
        			<th>Head3 </th>
        			<th>Head4 </th>
        		</tr>
        		<tr>
        			<td>11</td>
        			<td>12</td>
        			<td>13</td>
        			<td>14</td>
        		</tr>
        		<tr>
        			<td>15</td>
        			<td>16</td>
        			<td></td>
        			<td>18</td>
        		</tr>
        		<tr> 
        			<td>19</td>
        			<td></td>
        			<td>21</td>
        			<td>22</td>
        		</tr>
        		<tr>
        			<td></td>
        			<td>24</td>
        			<td>25</td>
        			<td>26</td>
        		</tr>
        	</table>

        【讨论】:

          猜你喜欢
          • 2012-05-20
          • 2017-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-26
          • 2019-01-30
          • 2011-02-07
          • 1970-01-01
          相关资源
          最近更新 更多