我想在现有的空 iframe 中附加整个页面(包含 <html><head>
和 <body>
元素)。
有可能吗?
据我所知,他们总是将内容附加到 iframe 中的某个子元素,例如 body
:
$('#demo').contents().find('body').html('aaa');
但我正在尝试追加整个页面,包括样式、标题等,因此我需要将其追加到文档级别。 像这样:
//not working
$('#demo').contents().html('<html><body>aaaa</body></html>');
最佳答案
您可以使用文档 write()
方法,如下所示:
var iframeDoc = $('iframe')[0].contentDocument; // jQuery is just for the example of course
iframeDoc.write(data);
其中data是一个完整有效的页面源字符串,包括DOCTYPE、HEAD等...
-DEMO-
关于javascript - 将整个页面附加到 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31474238/