可能:将我自己的HTML(整个网站)插入iframe [英] Possible: Insert my own HTML(whole website) into an iframe

查看:175
本文介绍了可能:将我自己的HTML(整个网站)插入iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站HTML存储在一个javascript字符串&我想把它放在iframe里面。 如何将该HTML字符串放入iframe&让它显示网站?我试过this.src = HTML_STR; this.innerHTML = HTML_STR;

I have all a websites HTML stored in a javascript string & I want to place it inside an iframe. How can I place that HTML string into the iframe & make it show the website? I have tried this.src = HTML_STR; this.innerHTML = HTML_STR; but they dont show the HTML_STR.

我理解这可能是愚蠢的,因为我不是在一个字符串中有网站HTML,我可以设置iframes src网站&以这种方式显示网站。

I understand that this maybe silly because instead of having the websites HTML in a string I could just set the iframes src to the website & show the website that way.

我这样做的原因是因为它的网站更新。所以我想得到的网站(我正在更新)HTML,将所有的元素类可更新转换为textareas(以便可以更新),然后在iframe中显示HTML。

BUT the reason I am doing this is because its a website updater. So I want to get the website (that I am updating) HTML, convert all the elements with the class "updatable" to textareas(so they can be updated) then show that HTML in the iframe.

使用iframe&不只是把HTML嵌入div,是因为每个网站导入一个.css样式表&我不想让他们的css文件影响我的更新网站css或有我的更新.css影响网站HTML看,因此如果我在iframe中显示网站每个网站css不会影响对方。

The other reason for using an iframe & not just chucking the HTML in a div, is because each website imports a .css style sheet & I dont want their css file to affect my updater websites css or have my updater .css affect the websites HTML look thus if I show the website in an iframe each websites css wont affect each other.

例如,如果updater.css具有以下内容,它将影响其他网站html中的所有p个元素:

For example if updater.css has the following it will affect all the p elements in the other websites html:

p { background-color: red; width: 50%; }

因此,有一种方法可以将自己的HTML插入iframe。

So is there a way to insert my own HTML into an iframe. Or is there another way to achieve what I am trying to do.

推荐答案

假设你想从iframe中写入iframe, parent,你可以这样做:

Assuming that you want to write to the iframe from the parent, you could do this:

var ifrm = document.getElementById('myFrame');
ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
ifrm.document.open();
ifrm.document.write('Hello World!');
ifrm.document.close();

这里是 a working jsFiddle 。原始代码取自此处此处

Here's a working jsFiddle. Original code taken from here and here.

这篇关于可能:将我自己的HTML(整个网站)插入iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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