[英]How do I start a new line in Javascript without using document.write()?
var infoDiv = document.getElementById("info"); console.log(window); console.log(window.location); infoDiv.textContent += "The URL of this page is " + window.location.href; // i am hoping the following sentence can start in a new line infoDiv.textContent += "The user agent is " + window.navigator.userAgent;
I tried document.write("\\n")
as people suggested here , but Chrome gives me this violation warning: 我按照人们在这里建议的方式尝试了document.write("\\n")
,但是Chrome给了我这个违规警告:
[Violation] Avoid using document.write(). [违规]避免使用document.write()。 https://developers.google.com/web/updates/2016/08/removing-document-write https://developers.google.com/web/updates/2016/08/removing-document-write
What should I do? 我该怎么办? Thanks! 谢谢!
Line breaks in HTML are created with the <br>
element (generally between text nodes). HTML中的换行符是使用<br>
元素创建的(通常在文本节点之间 )。 Paragraphs with the <p>
element (generally around text nodes). 带<p>
元素的段落(通常在文本节点周围 )。
In either case you'll need to stop twiddling textContent
because it will wipe out any elements you create. 在这两种情况下,您都需要停止搅动textContent
因为它将清除您创建的所有元素。
Use document.createElement
, document.createTextNode
, and infoDiv.appendChild
. 使用document.createElement
, document.createTextNode
和infoDiv.appendChild
。
You can append elements instead of text: 您可以附加元素而不是文本:
var infoDiv = document.getElementById("info");
infoDiv.append("The URL of this page is " + window.location.href);
// append new line
infoDiv.append(document.createElement('br'));
infoDiv.append("The user agent is " + window.navigator.userAgent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.