简体   繁体   English

如何在变量中换行

[英]How can I make a newline within a variable

I'm pretty new to Javascript and I ran into a problem.我是 Javascript 的新手,遇到了问题。 I tried searching it up to find out how to resolve it but I just don't know the specific terms to search so I couldn't find a solution.我尝试搜索它以找出解决方法,但我只是不知道要搜索的具体术语,所以我找不到解决方案。 I was wondering how to add newlines within a variable so that when you render the variable with HTML, it shows it separate by lines, not just "\n" written out.我想知道如何在变量中添加换行符,以便当您使用 HTML 呈现变量时,它会按行显示它,而不仅仅是写出“\n”。 For example,例如,

let foo = "bob<br>is<br>cool"
return (
    <div>
        {foo}
    </div>
)

In the browser, this shows as "bob< b r >is < b r >cool", without the spaces.在浏览器中,这显示为“bob< b r >is < b r >cool”,没有空格。 How can I format it so that it shows correctly as bob我如何格式化它才能正确显示为 bob
is
cool?凉爽的? Also, how can I write "bob< b r >is < b r >cool" without the spaces but without having the formatting?另外,如何在没有空格但没有格式的情况下编写“bob< b r >is < b r >cool”?

Is it possible to concatenate parts to it as well?是否也可以将零件连接到它? How would I do我该怎么办

foo += "hello<br>"

Thanks, sorry if this concept is simple because I somehow didn't stumble upon the solution while searching so I turned to here谢谢,抱歉,如果这个概念很简单,因为我在搜索时不知何故没有偶然发现解决方案,所以我转向这里

It's React?是反应?

use dangerouslySetInnerHTML危险地使用dangerouslySetInnerHTML

<div dangerouslySetInnerHTML={{ __html: foo }}></div>

use dangerouslySetInnerHTML for React对 React 使用dangerouslySetInnerHTML

 <div dangerouslySetInnerHTML={{__html: foo}}> ...

If it is REACT,如果是反应,

use dangerouslySetInnerHTML危险地使用 SetInnerHTML

just like <div dangerouslySetInnerHTML={{ __html: foo }}></div>就像<div dangerouslySetInnerHTML={{ __html: foo }}></div>

here is example below,这是下面的例子,

function App(){
  const markup={ __html: 'This text is set using </br>dangerouslySetInnerHTML'}
return(
<div dangerouslySetInnerHTML={markup}>
  
  </div>)
  }
ReactDOM.render(<App />, document.querySelector("#root"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM