Javascript 为什么'\n';是否不使用ServiceNow脚本?未创建新行
我们已经尝试了Javascript 为什么'\n';是否不使用ServiceNow脚本?未创建新行,javascript,servicenow,Javascript,Servicenow,我们已经尝试了'\n'的几种变体,并尝试了'\r',但脚本总是同时返回行或在行之间加空格,而不是实际的新行。下面是底部两个问题的脚本: (function(current, previous, gs, action) { var eqStr ='cmdb_ci=' + current.getValue('cmdb_ci'); //eqStr='short_description=' + current.getValue('short_description'); //eqStr
'\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') ;
如果要构建的字符串最终将被注入到DOM元素中,则必须使用负责解析DOM元素的HTML解析器熟悉的代码。HTML解析器不知道JavaScript字符串转义码,如下所示:
let output=“这是一个测试。”;
document.querySelector(“div”).innerHTML=输出;
let output2=“这是另一个测试。”;
document.querySelector(“p”).textContent=output2代码>
Its'\n
和\r
不是/n
和/r
,最后字符串将输出到哪里?如果它被注入到DOM元素中,您将得到您想要的结果,因为\n
不是导致HTML换行的方式,
是。这些代码只有JS运行时才能理解。@StephenP您不应该像以前那样编辑/更改帖子的内容。您假设正斜杠是打字错误,而它们可能不是。如果你在问题中纠正了它们,你可能只是在不知不觉中解决了问题。@ScottMarcus文章标题和文本中的斜杠是/n
——但代码中的斜杠是正确的反斜杠。我大体上同意你的看法,但在这种情况下,似乎很明显是问题的输入出错了,而不是代码。这个eqStr
字符串后来是如何使用的?在
/
中,输入元素
?我不小心在标题中添加了/n,但代码是正确的,\n。那么在这种情况下,我该如何使用
呢?嗨,斯科特,我们最后用它作为最后两行-eqStr+='^justization='+current.getValue('number')+%0d%0a“+current.getValue('short_description')+%0d%0a”+current.getValue('description');