2013-09-05 65 views
0

我正在處理將顯示數據庫字段內容的頁面。內容爲HTML格式,包括整個網頁替換iframe文檔的HTML源碼

<html> 
    <title>Some title</title> 
    <style type="text/css"> 
    /* CSS content here */ 
    </style> 
    <body> 
    <!-- body content here --> 
    </body> 
</html> 

我需要顯示所有這一切另一個網頁裏面,所以我想用一個iframe,讓我來否定父頁面的任何CSS。但是,我無法弄清楚如何覆蓋iframe文檔的內容。

什麼我目前做的是檢索數據,然後使用以下

$("#iframe_constainer") 
    .find('iframe:first').contents().find('html:first').html(myHTML); 

我的網頁包括一個div叫iframe_container其中包含一個iframe。

<div id="iframe_container"><iframe/></div> 

這是工作好,但myHTML內容越來越包裹在裏面html標籤。所以效果是這樣的

<html> 
    <html> 
    <title>Some title</title> 
    <style type="text/css"> 
     /* CSS content here */ 
    </style> 
    <body> 
     <!-- body content here --> 
    </body> 
    </html> 
</html> 

我知道這是因爲我在iframe中找到html標籤並更改了HTML。有沒有辦法使用jQuery覆蓋整個文檔?

回答

0

您並不需要jQuery,只需使用document.write即可將內容寫入iframe以完全替代iframe的文檔。

E.g. (普通香草JS)

document.getElementById("myIframeId").contentWindow.document.write(myHTML); 
+1

這不會替代,而是附加到iframe文檔 – Kurren