Javascript jsx 表忽略的换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29401711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:18:30  来源:igfitidea点击:

newlines character ignored by jsx table

javascriptreactjsreact-jsx

提问by Eric Baldwin

I am trying to create a table with a multiline string, but the string is not formatted correctly by my table. Here is the jsx:

我正在尝试使用多行字符串创建一个表,但是我的表没有正确格式化该字符串。这是jsx:

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

And here is the corresponding html:

这是相应的html:

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

But in the browser it looks like:

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

enter image description here

在此处输入图片说明

What's going on and how do I get my newlines to appear?

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

采纳答案by WiredPrairie

You've got a few options:

你有几个选择:

1) Use a block level element such as divor a pand wrap each line.

1) 使用块级元素,例如div或 ap并包裹每一行。

var TextLines = React.createClass({      
    render: function() {
        var lines = this.props.lines;

        var formatted = lines.map(function(line) {
            return (<p>{line}</p>);
        });
        return (<div>{ formatted }</div>);
    }
});

var lines = ['line 1', 'line 2', 'line 3'];
React.render(<TextLines lines={ lines }/>, 
              document.getElementById('container'));

2) Use a span with a brelement:

2)使用带有br元素的跨度:

var TextLines = React.createClass({      
    render: function() {
        var lines = this.props.lines;

        var br = lines.map(function(line) {
            return (<span>{line}<br/></span>);
        });
        return (<div>{ br }</div>);
    }
});

var lines = ['line 1', 'line 2', 'line 3'];
React.render(<TextLines lines={ lines } />,  
              document.getElementById('container'));

3) If you're certain there is no threat of XSS/hacks with the data, you could use dangerouslySetInnerHTMLwith a 'br' for each line:

3) 如果您确定数据没有 XSS/hacks 的威胁,您可以dangerouslySetInnerHTML为每一行使用一个 'br':

var TextLines = React.createClass({      
    render: function() {
        var lines = this.props.lines;
        var content = {
            __html: lines.join('<br />')
        };     
        return (<div dangerouslySetInnerHTML={ content } />);
    }
});

var lines = ['line 1', 'line 2', 'line 3'];
React.render(<TextLines lines={ lines } />, 
             document.getElementById('container'));

The last one produces the least amount of HTML, but at a cost of potentially risky the security of the web page/user. I wouldn't use this one if the others work for you.

最后一个生成的 HTML 最少,但代价是可能危及网页/用户的安全。如果其他人为你工作,我不会使用这个。

回答by Chris

Try white-space: pre;or white-space: pre-wrap;(Thanks, @Mirage) in the style of your cell.

尝试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>

回答by dontmentionthebackup

When you really need this, use {'\n'}inside your JSX.

当你真的需要这个时,{'\n'}在你的 JSX 中使用。

回答by klioen

try to use the <pre>element or the css property white-space: pre-wrap;

尝试使用<pre>元素或 css 属性white-space: pre-wrap;

回答by flaviodesousa

This is an HTML thing where newlines are the same thing as spaces. To force newlines use <br/>tags like {arr.join('<br/>')}.

这是一个 HTML 的东西,其中换行符与空格相同。要强制换行,请使用<br/>诸如{arr.join('<br/>')}.