简体   繁体   English

换行符用于console.log()但不是chrome浏览器

[英]Newline character working for console.log() but not chrome browser

I'm concatenating a bunch of strings from an array together with a new line character after each iteration: 我在每次迭代后将一串字符串与一个新的行字符连接在一起:

booksList = booksList + library.shelves[i].books[j].name + ", " +
   library.shelves[i].books[j].author + '\n';

When I console.log() to chrome it works, but when I use jquery to set the text of a div it doesn't. 当我将console.log()转换为chrome时,它可以正常工作,但是当我使用jquery来设置div的文本时它不会。 I'm on a windows comp. 我正在使用Windows comp。 Any ideas? 有任何想法吗?

I'm using jquery to set booksList to the text of a div: 我正在使用jquery将booksList设置为div的文本:

$('#libraryDisplay').text(booksList);

You can put a style on the div like this: 你可以在div上放一个样式:

$('#libraryDisplay').css('white-space', 'pre-wrap');

Of course <br> also works as it was answered before: 当然, <br>也可以像以前一样回答:

You should use <br> for new lines in HTML. 您应该在HTML中使用<br>作为新行。 Spaces and new lines are used in HTML for formatting of the code and making it human readable and don't do much for markup itself. HTML中使用了空格和新行来格式化代码并使其具有人类可读性,并且对标记本身没有太大作用。

And you should also use jquery's html() function to insert html to a page, because text() acts like htmlspecialchars in a way, replacing all the symbols that create html markup with their text variants. 你还应该使用jquery的html()函数将html插入页面,因为text()在某种程度上就像htmlspecialchars一样,用它们的文本变体替换所有创建html标记的符号。

booksList = booksList + library.shelves[i].books[j].name + ", " +
                   library.shelves[i].books[j].author + '<br>';

$('#libraryDisplay').html(booksList);

Try to use 尝试使用

<br>

instead of \\n. 而不是\\ n。

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

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