英文:
what is the difference between ReactJS convert \r\n in this two way and why?
问题
为什么,谢谢
是因为一个是文本节点,另一个是变量。
答案1
得分: 2
<div>123
123</div>
<div>{'123\r\n123'}</div>
英文:
<div>123\r\n123</div>
The former one is pure HTML, i.e. what is between the opening and closing tags will be displayed as is.
<div>{'123\r\n123'}</div>
Whereas the latter one is JSX, where the inner content is a string (programmatically speaking) - notice the use of <kbd>{</kbd><kbd>}</kbd> in the tags - thus \r
and \n
being escape characters
答案2
得分: 1
那些特殊字符在HTML中不起作用,字面文本'\r\n'将在浏览器中呈现。
使用反引号 `
字符创建一个模板字符串,它比原始HTML文本具有更多的'syntactic sugar'和功能。它是一个JavaScript字符串,你可以在其中使用JS特殊字符(例如\r\n
)。
HTML使用字符编码,看起来像这样:&#13;
。这代表了\n
'newline'字符,并将在你的示例中起作用。
英文:
Those special characters don't work in HTML, the literal text '\r\n' will be rendered in the browser.
Using the backtick `
character creates a Template String, which has a bit more 'syntactic sugar' and functionality than raw HTML text. It is a Javascript string and you can use JS special characters (e.g. \r\n
) inside it.
HTML uses character encodings instead, which look like this: &#13;
. This represents the \n
'newline' character, and will work in your example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论