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