我嘗試在對象元素(type =「text/html」)內調用document.open/write/close序列。在Safari/Chrome中,我可以通過contentDocument屬性獲取內部文檔對象。示例代碼(使用jQuery):JavaScript:如何檢索HTML對象標籤的文檔對象?
$(document).ready(function() {
var container = $('<object/>')
.css({'width': '700px', 'height': '100px', 'border': '0px none'})
.attr({'type': 'text/html'}).appendTo('body');
var doc = container.get(0).contentDocument;
doc.open();
doc.write('<h1>Hello world!</h1>');
doc.close();
});
有沒有辦法做到在其他瀏覽器一樣的嗎?
我想做這種奇怪的事情的原因是我需要在DOM關閉後調用包含document.write的外部腳本。我已經嘗試過處理iframe,但是由於Internet Explorer和Opera錯誤,我失敗了。任何其他方式來實現這一目標將不勝感激。
什麼錯誤?並且該對象的src是最初相同的原點嗎? – mplungjan 2013-03-26 18:05:10
http://stackoverflow.com/questions/1736886/why-wont-this-javascript-using-document-open-and-document-write-work-in-inter Bobince評論解釋了錯誤。 IE9仍然受到影響,最終可能會崩潰。 – pamelus 2013-03-26 18:15:22