javascript - jsx 表忽略的换行符

标签 javascript reactjs

我正在尝试创建一个包含多行字符串的表格,但我的表格中的字符串格式不正确。这是 jsx:

<td>
  {arr.join('\n')}
</td>

这是相应的html:

<td data-reactid=".xyz">Line 1
Line 2
Line 3
Line 4</td>

但在浏览器中它看起来像:

enter image description here

发生了什么事以及如何让我的换行符出现?

最佳答案

在您的单元格样式中尝试 white-space: pre;white-space: pre-wrap;(谢谢,@Mirage)。

td {
  width: 150px;
}

.nopre {
  background-color: lightblue;
}

.withpre {
  background-color: lightgreen;
  white-space: pre;
}

.withprewrap {
  background-color: orange;
  white-space: pre-wrap;
}
<table><tr>

<td class="nopre">Line A
Line B
Line C
This is a cell with no whitespace setting</td>

</tr></table><table><tr>

<td class="withpre">Line 1
Line 2
Line 3
This is a cell with white-space: pre</td>

</tr></table><table><tr>
  
<td class="withprewrap">Line 1
Line 2
Line 3
This is a cell with white-space: pre-wrap</td>
  
</tr></table>

关于javascript - jsx 表忽略的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29401711/

相关文章:

html - 如何使用 React/Angular/Vue 使 </div> 更接近其 <div>?

testing - React 的 Jasmine 测试导致 Webpack 构建失败

javascript - 数组映射不返回值 react

javascript - 获取本地存储值并在 redux 中切换存储

node.js - react | Node 应用| Docker部署

javascript - 更改 yeoman 的应用程序文件夹名称

JavaScript - 无法正确访问对象内的属性

javascript - 在 jquery 中切换 div 背景之前加载图像

javascript - 防止点击标识符时出现多次延迟

javascript - 未捕获的类型错误 : Cannot read property 'appendChild' of null with onsen ui