设置 iframe 的innerHTML [英] set innerHTML of an iframe

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

问题描述

在javascript中,如何设置iframe的innerHTML?我的意思是:如何设置,而不是 get.

In javascript, how can I set the innerHTML of an iframe? I mean: how to set, not get.

window["ifrm_name"].document.innerHTML="

Hi

" 不起作用,其他解决方案也一样.

window["ifrm_name"].document.innerHTML= "<h1>Hi</h1>" does not work, and the same for other solutions.

iframe 和父文档在同一个域中.

Iframe and parent document are on the same domain.

我需要设置整个文档 iframe 的 html,而不是它的正文.

I would need to set html of the whole document iframe, not its body.

我需要避免使用 jquery 解决方案.

I would need to avoid jquery solution.

推荐答案

一个非常简单的例子...

A really simple example ...

<iframe id="fred" width="200" height="200"></iframe>

然后运行以下 Javascript,内联、事件的一部分等...

then the following Javascript is run, either inline, part of an event, etc ...

var s = document.getElementById('fred');
s.contentDocument.write("fred rules");

contentDocument"相当于您在主窗口中获得的文档",因此您可以调用它来设置正文、头部、内部的任何元素等.

the "contentDocument" is the equivalent of the "document" you get in the main window, so you can make calls against this to set the body, head, any elements inside ... etc.

我只在 IE8、Chrome 和 Firefox 中测试过这个……所以如果你有可用的副本,你可能想在 IE6/7 中测试.

I've only tested this in IE8, Chrome and Firefox ... so you may want to test in IE6/7 if you have copies available.

这篇关于设置 iframe 的innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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