contentWindow : IFrame與window對象

轉自:https://blog.csdn.net/dongzhiquan/article/details/5851201

var detialIframe=document.all("detialIframe");

    此處的IFrame是從document取得的,即作作爲document的子對象出現,雖然是文檔(document)對象,但由於它是獨立的頁面,因而擁有自己的事件,擁有自己的窗口對象(contentWindow); Window.detialIframe 或Window.frames(detialIframe)將直接取得IFrame的Window對象

IFRAME

    IFRAME 元素也就是文檔中的文檔

window 對象

    瀏覽器會在其打開一個 HTML 文檔時創建一個對應的 window 對象。但是,如果一個文檔定義了一個或多個框架(即,包含一個或多個 frame 或 iframe 標籤),瀏覽器就會爲原始文檔創建一個 window 對象,再爲每個框架創建額外的 window 對象。這些額外的對象是原始窗口的 子窗口,可能被原始窗口中發生的事件所影響。例如,關閉原始窗口將導致關閉全部子窗口。如果想要創建新窗口(以及對應的 window 對象),可以使用像 open, showModalDialog 和 showModelessDialog 這樣的方法。

contentWindow

    contentWindow屬性是指指定的frame或者iframe所在的window對象

    在IE中iframe或者frame的contentWindow屬性可以省略,但在Firefox中如果要對iframe對象進行編輯則必須指定contentWindow屬性。

function EnableEdit()
{
    var editor;
    editor = document.getElementById("HtmlEdit").contentWindow;
  // 針對IE瀏覽器, make it editable
    editor.document.designMode = 'On';
    editor.document.contentEditable = true;
  // For compatible with FireFox, it should open and write something to make it work
    editor.document.open();
    editor.document.writeln('<html><head>');
    editor.document.writeln('<style>body {background: white;font-size:9pt;margin: 2px; padding: 0px;}</style>');
    editor.document.writeln('</head><body></body></html>');
    editor.document.close();
}

<iframe  ID="HtmlEdit" MARGINHEIGHT="1" MARGINWIDTH="1" width="100%" height="312">
</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>

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