javascript - 如何在 javascript string 中放置 html 标签?

我在这里有这段代码:

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 元素查看时,所有标签都消失了,并留下了 input、xinput、input 变量。

当作为 P 元素查看时,如何让所有这些都保留下来?

回答1

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

如果您想将其视为纯文本,请https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

相似文章

随机推荐

最新文章