動態生成(改變)iframe中的內容

 

動態生成(改變)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[]才能獲取到。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章