我在 Javascript/JQuery 脚本中得到一个 HTML 字符串,它是文档的完整 HTML。我需要在 <div>
中显示此 HTML在我的网页中。
例如,我的 Javascript 是这样的:
// code ...
var htmlString = "<!DOCTYPE html><html><head><title>something</title>...other html tags...</head><body>...</body></html>";
// code...
我需要在特定的 var htmlString
中渲染 html 字符串( <div>
的值)在我的网页中。
我的想法是,为了在另一个HTML页面中显示一个HTML页面,我们需要使用iframe
。但是iframe
获取托管在某处的网页的链接。我所拥有的只是一个完整HTML网页的HTML字符串,我需要将其显示在我的网页上<div>
中.
我怎样才能做到这一点?
最佳答案
您可以加载一个没有 src 的空文件,然后使用脚本将内容应用到它。
<iframe></iframe>
document.querySelector('iframe')[0]
.contentDocument.write("<h1>Injected from parent frame</h1>")
参见:Iframe without src but still has content? 希望对你有帮助
关于javascript - 如何填充一个完整文档的 HTML 字符串的 iframe,而不是在 src 属性中为其提供指向另一个网页的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51686486/