如何在 javascript 字符串中放置 html 标签?

How to place html tags inside javascript string?

我这里有这段代码:

generation.innerHTML=
"<!DOCTYPE html>\n"+
"<html>\n"+
 "<head>\n"+
  "<style>\n"+
   cinput+
  "\n<\/style>"+
 "\n<\/head>"+
 "<body>\n"+
  hinput+
  "\n<script>\n"+
   jinput+
  "\n<\/script>\n"+
 "<\/body>"+
"<\/html>";

但是当我控制台记录它时,doctype、html、head 和 body 标签消失了,但是当它作为 P 元素查看时,所有标签都消失了,留下 hinput、cinput、jinput 变量.

作为 P 元素查看时,如何使所有内容都保留?

innerHTML 尝试将数据解析为 HTML。因此得名。

如果要将其视为纯文本,use textContent instead