我们尝试了'\n'
的几种变体,并尝试了'\r'
,但是脚本会不断地将这些行一起返回或在它们之间留有空格,而不是实际返回新行。这是底部有问题的脚本:
(function(current, previous, gs, action) {
var eqStr ='cmdb_ci=' + current.getValue('cmdb_ci');
//eqStr='short_description=' + current.getValue('short_description');
//eqStr += '^description=' + current.getValue('description');
eqStr +='^priority=' + current.getValue('priority');
eqStr +='^sys_domain=' + current.getValue('sys_domain');
eqStr +='^company=' + current.getValue('company');
eqStr +='^justification=' + current.getValue('number')
+ '\n' + current.getValue('short_description')
+ '\n' + current.getValue('description') ;
答案 0 :(得分:1)
如果要构建的字符串最终被注入DOM元素,则必须使用负责解析DOM元素的HTML解析器熟悉的代码。 HTML解析器不了解JavaScript字符串转义代码,如下所示:
let output = "This is\na test.";
document.querySelector("div").innerHTML = output;
let output2 = "This is\nanother test.";
document.querySelector("p").textContent = output2;
<div></div>
<p></p>
对于将成为DOM元素一部分的字符串,您需要使用创建换行符的HTML标签<br>
:
let output = "This is<br>a test.";
// innerHTML invokes the parser to parse the supplied string:
document.querySelector("div").innerHTML = output;
let output2 = "This is<br>another test.";
// This won't produce the line break because textContent doesn't invoke the HTML
// parser. Insted, the actual text will be placed in the string.
document.querySelector("p").textContent = output2;
<div></div>
<p></p>