替换为iframe以呈现完整的html文档

时间:2016-07-16 20:29:09

标签: jquery html ajax html5 iframe

有没有办法在html标记(而不是iframe)中呈现一个完整的html文档,该文档也有脚本和样式标题。我将此html文档作为AJAX帖子请求的响应。

1 个答案:

答案 0 :(得分:0)

您可以使用<template>元素element.attachShadowRoot()。见Shadow DOM。注意Browser compatibility

<!DOCTYPE html>
<html>

<body>
  <template>
    <!DOCTYPE html>
    <html>

    <head>
      <style>
        :host {
          color: blue;
        }
      </style>
    </head>

    <body>
      abc
      <script>
        console.log("shadowDOM")
      </script>
    </body>

    </html>
  </template>
  <script>
    var shadow = document.body.attachShadow({mode:"open"});
    var template = document.querySelector("template");
    var shadowContent = document.importNode(template.content, true);
    shadow.appendChild(shadowContent);
  </script>
</body>

</html>

相关问题