【问题标题】:How to easily add/remove cells from a Table (Shifting existent cells)如何轻松地从表格中添加/删除单元格(移动现有单元格)
【发布时间】:2016-01-17 02:28:59
【问题描述】:

我希望表格自动将单元格对齐为每行两个,这意味着如果添加/删除一个单元格,单元格将变为每行两个(如果单元格总数为奇数,则最后一行有一个)。

例子:

<table>

<tr>
<td>old1</td> <td>old2</td>
</tr>


<tr>
<td>old3</td> <td>old4</td>
</tr>

</table>

到这里:

<table>

<tr>
<td>NEW</td> <td>old1</td>
</tr>


<tr>
<td>old2</td> <td>old3</td>
</tr>


<tr>
<td>old4</td>
</tr>

</table>

注意自动添加的

【问题讨论】:

  • 为此,您需要使用 JavaScript,或使用 HTML 表格以外的其他内容来包含要显示的内容,例如带有 &lt;li&gt; 子元素的列表。
  • 我不明白你在问什么或问题是什么。
  • 你想让它看起来像一个博客吗?你把第一个放在上面,把“旧新闻”放在下面?

标签: css html html-table cell


【解决方案1】:
table {
   empty-cells: hide;      
}

【讨论】:

    【解决方案2】:

    我将向你展示三种方法来完成你所需要的,

    • 使用 flex(仅限现代浏览器)
    • 使用 inline-tablecell height 的问题,但可用作 sort-of 后备)
    • 使用标准 &lt;table&gt; (所有浏览器!)

    使用 CSS3 的 FLEX

    .table{
      display: flex;
      flex-wrap: wrap;
    }
    .cell{
      width:50%;
      background:#ddd;
    }
    <div class="table">
      <div class="cell">NEW</div>
      <div class="cell">OLD1</div>
      <div class="cell">OLD2</div>
      <div class="cell">OLD3<br>new line</div>
      <div class="cell">OLD4</div>
    </div>

    使用inline-table(单元格高度问题)

    display: table;   用于父元素,
    display: inline-table用于内部 DIV:

    .table{
      display:table;
    }
    .cell{
      display:inline-table;
      width:50%;     /* With percentage you simulate "how-many-per-Row" */
      background:#ddd;
    }
    <div class="table">
      <div class="cell">NEW</div>
      <div class="cell">OLD1</div>
      <div class="cell">OLD2</div> <!-- Works but notice this cell height  -->
      <div class="cell">OLD3<br>new line</div> 
      <div class="cell">OLD4</div>
    </div>

    您可以使用 inline-table 作为旧浏览器的后备选项 - 这不是完全相同的结果,但如果没有 JavaScript,它是您可以获得的最佳结果。

    查看有关 display 属性的规范:https://developer.mozilla.org/en-US/docs/Web/CSS/display


    使用 &lt;table&gt;&lt;tr&gt;&lt;td&gt; 和 JavaScript (jQuery)

    如果你想坚持使用好的 'ol table 元素,你可以使用“一点”的 jQuery。
    在那种情况下,它有点复杂(参见 code-cmets):

    jQuery(function($){ // DOM is now ready
    
      var $table = $("table"); // Get your table
      var maxRowCells = 2;     // only 2-per-Row
      var counter = 0;         // just a dummy counter
    
      function prependNewCell(){
    
        var $newCell = $("<td/>", {html: "NEW"+(++counter)});
    
        // Is the last row full?
        if($table.find("tr:last td").length == maxRowCells){
          // If true, append a new TR to TABLE to shift TD's to
          $table.append( $("<tr/>") ); 
        }
    
    
        // Shift all last TD's to the next row:
        $table.find("tr:not(:last)").each(function(){
          var $tdLast = $(this).find("td").last();
          $(this).next("tr").prepend( $tdLast );
        });
    
        // Finally. Prepend the new TD element to the first TR
        $table.find("tr").first().prepend( $newCell );
    
      }
    
      $("button").on("click", prependNewCell);
    
    });
    td{
      width:50%;
      background:#ddd;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button>ADD NEW CELL ON TOP</button>
    
    <table>
      <tr>
        <td>OLD1</td>
        <td>OLD2</td>
      </tr>
      <tr>
        <td>OLD3</td>
        <td>OLD4</td>
      </tr>
    </table>

    【讨论】:

    • @DaniSpringer 认为这很明显。 &lt;br&gt; 换行符显然只是为了测试如果一个单元格内容超过一行时其他单元格的行为,因为前两个示例不使用本机 &lt;table&gt;。看到示例 1 和示例 2 的区别了吗?不? (看看示例 2 中左侧单元格的高度是如何没有扩大的?)
    • @DaniSpringer 为什么在 CSS 中定义.cell? ...这样您就可以对其进行样式设置并设置所需的width 认为这很明显。您不需要使用.cell,您可以使用不带类的&lt;div&gt; 并使用.table &gt; div 定位它们...
    • @dani 这只是一个内容示例,在演示中显示相关差异和可能的问题更加明显......谁强迫你使用换行符?您是否尝试过使用演示并删除换行符?拜托 - 我给了你很多超酷的例子,现在由你来探索和玩耍。
    • @Dani,使用我的示例最简单的方法可以立即看到结果,请使用jsbin.com。将我的代码示例粘贴到所需的 HTML/CSS 选项卡中 > 查看结果 > 播放!
    • @DaniSpringer 删除 .cell 样式 > 看看会发生什么,删除 width > 看看会发生什么。探索并度过愉快的时光!编码愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多