简体   繁体   English

如何在数组输出中添加换行符?

[英]How do I add line breaks to my array output?

var d0 = document.getElementById("d0").getElementsByTagName('h2');
var pp0 = document.getElementById("prepend0").getElementsByClassName('formDiv');
//alert(d0.length);
//alert(pp0.length);
var report ="";
for (var i = 0; i < d0.length; i++) {
    report += d0[i].textContent+"%0D%0A";//gets the date for day1
    for (var j = 0; j < pp0.length; j++) {
        report += pp0[j].textContent+"%0D%0A";//gets the d0 formDiv children. I want to out put this with line breaks
    }     
}

OutPut: 输出:

Sun Apr 19 2015 
Project OneEnter Description Here.Total Time: 00:00:00 

What I want to do is: 我想做的是:

Sun Apr 19 2015 
Project One
Enter Description Here.
Total Time: 00:00:00 

%0D%0A are url encoded versions of line breaks, and as such you should be seeing the literal %0D and %0A characters instead of "nothing" that your example shows. %0D%0A是换行符的url编码版本,因此,您应该看到的是原义%0D%0A字符,而不是示例中显示的“ nothing”。

For plaintext views, such as in a text file, "View Source" in a browser, or within a <pre> tag, you'll want to use \\r\\n instead: 对于纯文本视图(例如,在文本文件中,在浏览器中或在<pre>标记中的“查看源代码”中),您将希望使用\\r\\n代替:

report += d0[i].textContent + "\r\n";

However, if you want the newlines to display in HTML (outside of the <pre> tag), you'll want to use <br /> : 但是,如果希望换行符以HTML显示(在<pre>标记之外),则需要使用<br />

report += d0[i].textContent + "<br />";

if you gone to print to console: 如果您要打印到控制台:

console.log("1111\ncdcd")

and if its HTML: 以及其HTML:

$(element).text("AAAA <br/> BBBB <br/> CCCCC")

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

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