Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过js放置HTML标记_Javascript_Html - Fatal编程技术网

Javascript 通过js放置HTML标记

Javascript 通过js放置HTML标记,javascript,html,Javascript,Html,我需要简单地向用户显示节点服务器中的一些错误,这是我的代码: if(text.length > 1){ text = text.join("<br>"); console.log("array:",text); }else{ text = text[0]; } 并将其附加到页面中,问题是js将其转换为br,因此下一个错误不会显示在新行中,而是如下所示: some text br other text 从您提供的代码中不清楚您到底做错了什么,但以下是您

我需要简单地向用户显示节点服务器中的一些错误,这是我的代码:

if(text.length > 1){
    text = text.join("<br>");
    console.log("array:",text);
}else{
    text = text[0];
}
并将其附加到页面中,问题是js将其转换为br,因此下一个错误不会显示在新行中,而是如下所示:

some text br other text

从您提供的代码中不清楚您到底做错了什么,但以下是您尝试做的工作版本:


也许您可以从node以不同的格式发送HTML字符串,但这在客户端代码中都可以:

如果您使用的是Jquery,则可以使用:

function DecodeHtmlString(htmlString){
  return $('<div/>').html(htmlString).text();
}

如果你对这个问题投了反对票,你能至少告诉我为什么吗?你能显示用于将此文本发送到浏览器的代码吗?这可能是一个编码/解码问题,但我们需要为您的案例找到正确的函数。
var text = ['Hello', 'world!'];

if(text.length > 1){
    text = text.join("<br/>");
}else{
    text = text[0];
}

document.getElementById('message').innerHTML = text;
function DecodeHtmlString(htmlString){
  return $('<div/>').html(htmlString).text();
}
function DecodeHtmlString(htmlString){
  var temp = document.createElement("textarea");
  temp.innerHTML = htmlString;
  return temp.value;
}