[英]How do I get these HTML tags to render from javascript
Noob question alert! 新手问题提示! So, I've got this script, which loops through an array and adds a
<br>
tag to the end of each array item. 所以,我有这个脚本,它遍历一个数组并在每个数组项的末尾添加一个
<br>
标记。 But i dont know the proper way of displaying this output on my page. 但是我不知道在我的页面上显示此输出的正确方法。 Currently, when it loads the
<br>
tags show up on screen, whereas I want them to render as line-breaks. 目前,当它加载
<br>
标签时,它会显示在屏幕上,而我希望它们呈现为换行符。 It is outputting into a <textarea>
if that makes a difference. 如果有区别,它将输出到
<textarea>
。 Thanks a bunch. 谢谢你
var outputLinkText = document.getElementById('outputLinkText');
var outputStageOne = "";
for (var i = 0; i < arrayOne.length; i++) {
outputStageOne += (arrayOne[i] + "<br>");
}
if ( 'textContent' in timePlace ) {
outputLinkText.textContent = outputStageOne;
}
else {
outputLinkText.innerText = outputStageOne;
}
<textarea>
tags don't support <br>
tags (or any other HTML tags) within their contents. <textarea>
标记在其内容内不支持<br>
标记(或任何其他HTML标记)。 They only hold plain text. 它们仅包含纯文本。
You need to add "\\n"
as the separator instead. 您需要添加
"\\n"
作为分隔符。
(Strictly, it should be "\\r\\n"
but a "\\n"
on its own is usually sufficient) (严格来说,它应该是
"\\r\\n"
但是通常只要一个"\\n"
就足够了)
Yes the textarea is a difference, try this : 是的,textarea有所不同,请尝试以下操作:
"\\r\\n"
instead of "<br>"
"\\r\\n"
而不是"<br>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.