如何在JavaScript中添加换行符?
我需要使用javascript向网页添加文本,所以我按照如下方式完成 //创建名称为h1 var h=document.createElement'h1'; var t=document.createTextNode'Austin Chandler'; h、 儿童; document.body.appendChildh; //格式化h1标记 document.querySelector'h1'.style.color='red'; document.querySelector'h1'.style.fontfamine='Tahoma'; document.querySelector'h1'.style.textAlign='center'; //断线 文档。写入“\n”; //创建课程和章节号为h2 var h_2=document.createElement'h2'; var t_2=document.createTextNode“WEB 115-第0001节”; h、 附加childt_2; document.body.appendChildh_2; //格式化h2标记 document.querySelector'h2'。style.color='red'; document.querySelector'h2'。style.fontfamine='Garamond'; document.querySelector'h2'。style.fontStyle='italics'; document.querySelector'h2'.style.textAlign='center'; 文档。写入“\n”;只在我的文本Austin Chandler和WEB 115之间添加了一个小空格-第0001节不是换行符 输出应如下所示: 奥斯汀·钱德勒 WEB 115-第0001节 当前输出如下所示: Austin Chandler WEB 115-第0001节 如何添加换行符?在HTML中,换行符是,因为Javascript正在编写HTML,所以需要使用 此外,所有文本都在h1中,因为您将第二个文本添加到了错误的元素。见下文。 //创建名称为h1 var h=document.createElement'h1'; var t=document.createTextNode'Austin Chandler'; h、 儿童; document.body.appendChildh; //格式化h1标记 document.querySelector'h1'.style.color='red'; document.querySelector'h1'.style.fontfamine='Tahoma'; document.querySelector'h1'.style.textAlign='center'; //断线 文件。书写; //创建课程和章节号为h2 var h_2=document.createElement'h2'; var t_2=document.createTextNode“WEB 115-第0001节”; //您正在将第二个文本添加到第一个元素 h_2.0;T_2; document.body.appendChildh_2; //格式化h2标记 document.querySelector'h2'。style.color='red'; document.querySelector'h2'。style.fontfamine='Garamond'; document.querySelector'h2'。style.fontStyle='italics'; document.querySelector'h2'.style.textAlign='center' 在HTML中,换行符是,因为Javascript正在编写HTML,所以需要使用 此外,所有文本都在h1中,因为您将第二个文本添加到了错误的元素。见下文。 //创建名称为h1 var h=document.createElement'h1'; var t=document.createTextNode'Austin Chandler'; h、 儿童; document.body.appendChildh; //格式化h1标记 document.querySelector'h1'.style.color='red'; document.querySelector'h1'.style.fontfamine='Tahoma'; document.querySelector'h1'.style.textAlign='center'; //断线 文件。书写; //创建课程和章节号为h2 var h_2=document.createElement'h2'; var t_2=document.createTextNode“WEB 115-第0001节”; //您正在将第二个文本添加到第一个元素 h_2.0;T_2; document.body.appendChildh_2; //格式化h2标记 document.querySelector'h2'。style.color='red'; document.querySelector'h2'。style.fontfamine='Garamond'; document.querySelector'h2'。style.fontStyle='italics'; document.querySelector'h2'.style.textAlign='center' 尝试添加。它将添加一个中断尝试添加。这会增加一个休息时间这对我来说很有效:如何在JavaScript中添加换行符?,javascript,line-breaks,Javascript,Line Breaks,我需要使用javascript向网页添加文本,所以我按照如下方式完成 //创建名称为h1 var h=document.createElement'h1'; var t=document.createTextNode'Austin Chandler'; h、 儿童; document.body.appendChildh; //格式化h1标记 document.querySelector'h1'.style.color='red'; document.querySelector'h1'.style
<script>
document.write ('line1');
document.write ('<br />');
document.write ('line2');
</script>
这对我很有用:
<script>
document.write ('line1');
document.write ('<br />');
document.write ('line2');
</script>
使用document.body.appendChilddocument.createElementbr;而不是\n
而且,您正在将所有文本添加到h1。您希望h2元素中的第二个文本:
//创建名称为h1
var h=document.createElement'h1';
var t=document.createTextNode'Austin Chandler';
h、 儿童;
document.body.appendChildh;
//格式化h1标记
document.querySelector'h1'.style.color='red';
document.querySelector'h1'.style.fontfamine='Tahoma';
document.querySelector'h1'.style.textAlign='center';
//断线
document.body.appendChilddocument.createElementbr;
//创建课程和章节号为h2
var h_2=document.createElement'h2';
var t_2=document.createTextNode“WEB 115-第0001节”;
//此处将h更改为h2:
h_2.0;T_2;
document.body.appendChildh_2;
//格式化h2标记
document.querySelector'h2'。style.color='red';
document.querySelector'h2'。style.fontfamine='Garamond';
document.querySelector'h2'。style.fontStyle='italics';
document.querySelector'h2'.style.textAlign='center' 使用document.body.appendChilddocument.createElementbr;而不是\n
而且,您正在将所有文本添加到h1。您希望h2元素中的第二个文本:
//创建名称为h1
var h=document.createElement'h1';
var t=document.createTextNode'Austin Chandler';
h、 儿童;
document.body.appendChildh;
//格式化h1标记
document.querySelector'h1'.style.color='red';
document.querySelector'h1'.style.fontfamine='Tahoma';
document.querySelector'h1'。style.textAlign='center'
;
//断线
document.body.appendChilddocument.createElementbr;
//创建课程和章节号为h2
var h_2=document.createElement'h2';
var t_2=document.createTextNode“WEB 115-第0001节”;
//此处将h更改为h2:
h_2.0;T_2;
document.body.appendChildh_2;
//格式化h2标记
document.querySelector'h2'。style.color='red';
document.querySelector'h2'。style.fontfamine='Garamond';
document.querySelector'h2'。style.fontStyle='italics';
document.querySelector'h2'.style.textAlign='center';你试过了吗?@iamdlm我试过了。它似乎不起作用。除非我做错了。我在var t的名字后面添加了它,并尝试使用document.write;在换行评论下面。你试过了吗?@iamdlm我试过了。它似乎不起作用。除非我做错了。我在var t的名字后面添加了它,并尝试使用document.write;在换行注释下。使用document.write;?对我来说,它只创建输出为Austin ChandlerWEB 115-0001@Austinwc0402是的,如果你检查元素,你会发现在源代码中有一个。您的逻辑仍然有问题,我正在努力解决。使用document.write;?对我来说,它只创建输出为Austin ChandlerWEB 115-0001@Austinwc0402是的,如果你检查元素,你会发现在源代码中有一个。你的逻辑仍然有问题,我正在努力找出答案;应该也行。是的,在我纠正了h到h_2的错误后,它就行了。猜@Nicolas更快了,document.write;应该也能用。是的,在我纠正了h到h_2的错误后,它能用了。