设置iframe innerHTML而不加载页面(使用jquery) [英] Set iframe innerHTML without loading page in it (with jquery)

查看:1325
本文介绍了设置iframe innerHTML而不加载页面(使用jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iframe之前没有加载任何网页时动态设置iframe的内容。

I want to set the contents of an iframe dynamically, when no pages were loaded before in the iframe.

我这样做:

iframe = $('<iframe id="thepage"></iframe>');

iframeHtml = 'iframeHtml';
$('body').append(iframe);

iframe
.contents()
.html(iframeHtml);

但它不起作用,html仍为空。

But it is not working, the html is still empty.

推荐答案

填充框架内容的最佳方法是 document.write

the best way to populate frame contents is document.write

var dstFrame = document.getElementById('yourFrameId');
var dstDoc = dstFrame.contentDocument || dstFrame.contentWindow.document;
dstDoc.write(yourHTML);
dstDoc.close()

UPD:

var iFrame = $('<iframe id="thepage"></iframe>');
$('body').append(iFrame);

var iFrameDoc = iFrame[0].contentDocument || iFrame[0].contentWindow.document;
iFrameDoc.write('<p>Some useful html</p>');
iFrameDoc.close();

这篇关于设置iframe innerHTML而不加载页面(使用jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆