简体   繁体   English

通过javascript对象访问数据并将其打印在html页面上

[英]Accesing data through a javascript object and printing it on html page

I have a object in which each key has a string corresponds to it for ex { 0 :'hello\\nworld', 1:'\\njavascript\\nis\\nawesome' } now I want to print this in html page and for each \\n there should be a break line for example - 我有一个对象,其中每个键都有一个与之对应的字符串,例如ex { 0 :'hello\\nworld', 1:'\\njavascript\\nis\\nawesome' }现在我想在html页面和每个\\n例如应该有一个折线-

hello(new line)
world(new line)
javascript(new line)
is(new line)
awesome(new line)

as a new programmer I don't know hoe to do it any help would be appreciated 作为一个新的程序员,我不知道该做什么,将不胜感激

You can use String.prototype.replace() . 您可以使用String.prototype.replace() and regular expression.By using g flag in regex you can replace all the \\n with <br> . 和正则表达式。通过在正则表达式中使用g标志,您可以将所有\\n替换为<br>

 let obj = { 0 :'hello\\nworld', 1:'\\njavascript\\nis\\nawesome' } for(let key in obj){ let reg = new RegExp('\\n',"g"); document.body.innerHTML += obj[key].replace(/\\n/g,'<br>'); } 

\\n替换为<br/>并将其全部放置在任何可见的html标记中,它将显示您的文本,并在遇到<br/>时返回到行。

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

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