使用 Javascript 或 jQuery 将元素写入子 iframe [英] Write elements into a child iframe using Javascript or jQuery
本文介绍了使用 Javascript 或 jQuery 将元素写入子 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的事情:
<!doctype html>
<html>
<body>
<iframe id="someFrame"></iframe>
</body>
</html>
而且我想使用 jQuery 来编写元素,使完整的等效 HTML 如下所示:
And I would like to use jQuery to write elements such that the full equivalent HTML would be like this:
<!doctype html>
<html>
<body>
<iframe id="someFrame">
<!-- inside the iframe's content -->
<!-- <html><body> -->
<div>A</div>
<div>B</div>
<div>C</div>
<!-- </body></html> -->
</iframe>
</body>
</html>
或者,任何普通的 Javascript 都可以.
Alternatively, any plain-old-Javascript would be fine.
谢谢.
编辑:经过更多研究后,我似乎正在寻找 iframe 的 contentDocument 属性的 IE 等效项.内容文档"是 FF 支持的 W3C 标准,但 IE 不支持.(惊喜惊喜)
Edit: After a little more research, it seems I am looking for an IE-equivalent of the contentDocument property of an iframe. "contentDocument" is a W3C standard which FF supports, but IE does not. (surprise surprise)
推荐答案
两者都可以,只是目标不同:
You can do both, you just have to target differently:
var ifrm = document.getElementById('myIframe');
ifrm = ifrm.contentWindow || ifrm.contentDocument.document || ifrm.contentDocument;
ifrm.document.open();
ifrm.document.write('Hello World!');
ifrm.document.close();
这篇关于使用 Javascript 或 jQuery 将元素写入子 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文