[英]Add contents to a new empty iframe
I have a new iframe. 我有一个新的iframe。 It takes src and includes a completely empty page. 它需要src并包括一个完全空白的页面。 Now I want to fill it with data trough javascript. 现在,我想用数据槽javascript填充它。
In normal cases I can do this: 在正常情况下,我可以这样做:
var content = "Some content";
$("iframe").contents().find("body").html(content);
But this iframe is empty and has no body or html. 但此iframe为空,没有正文或html。 Is it possible to include content to it anyway? 反正有可能包含内容吗?
Something like this should be inserted: 应该插入以下内容:
<html>
<head></head>
<body>Hello</body>
</html>
Update 更新
I can do this: 我可以做这个:
$('iframe').contents().find('html').html('<html><body>test</body></html>');
I don't know if it will render double html tags or not. 我不知道它是否将呈现双HTML标签。 In the developer tool I can't see that it add an extra. 在开发人员工具中,我看不到它增加了额外的功能。
You can get hold of the IFrame window by a normal query selector. 您可以通过常规查询选择器来掌握IFrame窗口。
var myFrameDoc = document.getElementById('iframe_id').contentDocument;
If it has nothing, write it. 如果没有任何内容,请将其写入。
myFrameDoc.write('<html>');
myFrameDoc.write('<head>');
myFrameDoc.write('</head>');
myFrameDoc.write('<body>');
myFrameDoc.write('<div>Hello iFrame</div>');
myFrameDoc.write('</body>');
myFrameDoc.write('</html>');
There are more ways you can do this. 您可以通过更多方法来执行此操作。 I will leave it for you to realize. 我将它留给您实现。
In jQuery it seems to be no solution. 在jQuery中,这似乎没有解决方案。 Instead I needed to do it like this: 相反,我需要这样做:
var content = '<html><head></head><body>Hello</body></html>';
var iframe = document.getElementById( '#iframe_id' );
iframe.contentWindow.document.open()
iframe.contentWindow.document.write(content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.