代码之家  ›  专栏  ›  技术社区  ›  Bh00shan

以换行符换行表格单元格内容

css
  •  0
  • Bh00shan  · 技术社区  · 8 年前

    我有一个包含新行字符的字符串。当我试图将它绑定到表单元格时,它不会换行。 附件为参考截图。 在控制台窗口中,它按照预期正确显示,我希望在表td标记中显示相同的内容。

    enter image description here

    4 回复  |  直到 8 年前
        1
  •  1
  •   Balázs Varga    8 年前

    如果从数据库中获取字符串,有两种方法可以实现这一点:

    替换 \n 带HTML分隔符的:

    echo str_replace("\n", "<br>", $string);
    

    看 \n 是这样的:

    echo '<pre>' . $string . '</pre>';
    

    还有一个CSS解决方案,将此规则应用于容器:

    white-space:pre;
    
        2
  •  1
  •   zessx    8 年前

    white-space: pre; 可能是你想要的:

    td {
      white-space: pre;
    }
    <table style="width:50%">
      <tr>
        <td>Extended Error message</td>
        <td>Hi
    this is new line</td>
      </tr>
    </table>
        3
  •  0
  •   לבני מלכה    8 年前

    设置每行 div 如果你想,你可以设置 margin 到 left

    .new-line{
        margin-left: 5px;
    }
    <table style="width:100%">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th> 
        <th>Age</th>
      </tr>
      <tr>
        <td><div>hi</div><div class="new-line">this is new line</div></td>
        <td>Smith</td>
        <td>50</td>
      </tr>
      <tr>
        <td>Eve</td>
        <td>Jackson</td>
        <td>94</td>
      </tr>
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>80</td>
      </tr>
    </table>
        4
  •  0
  •   Ehsan    8 年前

    试试这个:

    <td>Hi<br>this is new line</td>
    

    <table border="1">
      <tr>
        <td>This is First</td>
        <td>Hi<br>this is new line</td>
      </tr>
    </table>

    或使用 pre 标签:

    <td>
        <pre>Hi
             this is new line
        </pre>
    </td>
    

    <table border="1">
      <tr><td>This is First</td><td><pre>Hi
       this is new line</pre></td></tr>
    </table>