现在的位置: 首页 > 综合 > 正文

动态生成(改变)iframe中的内容

2013年08月29日 ⁄ 综合 ⁄ 共 1222字 ⁄ 字号 评论关闭

 

动态生成(改变)iframe中的内容  

 

<html>
<body>
<script>
var ifr = document.createElement("iframe");
document.body.appendChild(ifr);
var ifrdoc = ifr.contentWindow.document;
var s = fixingHTB.innerHTML; //进入可编辑模式前存好
ifrdoc.designMode = "on"; //文档进入可编辑模式
ifrdoc.open(); //打开流
ifrdoc.write(s);
ifrdoc.close(); //关闭流
ifrdoc.designMode ="off"; //文档进入非可编辑模式
</script>
</body>
</html>

注:ifr.contentWindow.document.body.innerHTML可以读取到iframe连接页面的内容,理解了这个才是关键。

==================

另外一种实现

<HTML>

<script>

var i
=
0 ;   
//全局变量,记录当前有几个IFrame

function allSubmit()

{   

for (j=1;j
<=i;j++)
{ eval('iframe'+j+'.document.all("form1").submit();');

   }

}

function addIframe()

{
var If=document.createElement("iframe");
If.style.height
="100px";
If.style.width
="300px";
If.id
="iframe"+(++i);
//document.all("sp").innerHTML=document.all("sp").innerHTML+" <iframe id=iframe" +(++i)+ " src='' width=300 height=100> </iframe>";

document.all("sp").appendChild(If);//===========Dom
API to add Iframe

var doc1
= window.frames["iframe"
+ i].document;

//doc1.open();

doc1.write(" <HTML>this is test </HTML>");

}
</script>

<body
bgcolor="#ffffff" text="#000000"
>
<div
id = sp>

</div>

<br>
<br>

<input
type="button" onclick="allSubmit()" value="全部提交">

<input
type="button" onclick="addIframe()" value="增加IFrame">

</body>

</html>

注:在firefox下要用添加name属性,这样window.frames[]才能获取到。

抱歉!评论已关闭.