简体   繁体   English

我怎样才能让这个javascript函数发出一个换行符

[英]how can i make this javascript function emit to a newline

okay I have an a script running that process a lot of data basically I am using the line below 好吧我有一个脚本运行该过程很多数据基本上我使用下面的行

 document.getElementById("message").innerHTML = JSON.stringify(stringbuild.hello);

problem is every time this line is send to message textbox it just overwrites the previous line from a previous output . 问题是每次将此行发送到消息文本框时,它只会覆盖前一个输出中的上一行。

how can I make each output emit to a new empty line and keep any other previous data in the textbox 如何使每个输出发出一个新的空行并保留文本框中的任何其他先前数据

If the 'message' element is a regular HTML element (eg a <div> ), try would work: 如果'message'元素是一个常规的HTML元素(例如<div> ),那么尝试就可以了:

document.getElementById("message").innerHTML += "<br>" + JSON.stringify(stringbuild.hello);
  • The += is an ' augmented ' assignment operator which appends the current string with whatever value is provided to the right. +=是一个' 扩充 '赋值运算符,它将当前字符串附加到右边提供的任何值。
  • The <br> is an html line break character, used to make place the the new string directly underneath the previous string. <br>是一个html换行符,用于将新字符串直接放在前一个字符串下面。

For an input element <input type="text"> or <textarea> , use this: 对于输入元素<input type="text"><textarea> ,请使用:

document.getElementById("message").value += "\n" + JSON.stringify(stringbuild.hello);
  • The value property is used to access the current value of the input element, and should be used instead of innerHTML . value属性用于访问input元素的当前值,应该使用而不是innerHTML
  • The \\n is the escape sequence for a new line character. \\n是新换行符的转义序列

Demonstration 示范

In a <textbox> use \\n which is a newline character: <textbox>使用\\n ,这是一个换行符:

node = document.getElementById("message");
node.innerHTML += JSON.stringify(stringbuild.hello) + '\n';

Don't use <br /> in a <textbox> . 不要在<textbox>使用<br />

And add it always at the end of the line, or you'll have the first line being empty. 并且总是在该行的末尾添加它,或者您将第一行添加为空。

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

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