iframe:如何显示原始(纯文本)html?

iframe: How to display raw (plain text) html?

我想在 iframe 中将 HTML 字符串显示为 raw/plain 文本 HTML。

例如,显示“<b>Hello World</b>”(而不是 Hello World)。

我试过这样显示:

<iframe srcdoc="<pre><b>Hello World</b></pre>"></iframe>

但是没有用。它显示粗体“Hello world”文本。

我正在使用 Nuxt.js。

我错过了什么?

没有 iframe

<pre id="code"></pre>

<script>
  document.getElementById('code').textContent = '<b>Hello World</b>';
</script>

使用 iframe

<iframe id="codeFrame"></iframe>

<script>
  function writeToIframe(iframe, markup) {
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write('<pre id="code"></pre>');
    iframe.contentWindow.document.getElementById('code').textContent = markup;
    iframe.contentWindow.document.close();
  }

  writeToIframe(document.getElementById('codeFrame'), '<b>Hello World</b>');
</script>