JavaScript對iframe的DOM操作


在IE6、IE7中,我們可以使用 document.frames[ID].document 來訪問iframe子窗口中的document對象,可是這是不符合W3C標准的寫法,也是IE下獨有的方法,在Firefox下卻不可以使用,Firefox下使用的是符合W3C標准的 document.getElementById(ID).contentDocument 方法,今天我在寫實例的時候,通過IE8進行測試,IE8也是使用的符合W3C標准的 document.getElementById(ID).contentDocument 方法。所以我們可以寫一個在IE和Firefox下通用的獲取iframe document對象的函數—getIFrameDOM:

1 function getIFrameDOM(id){
2     return document.getElementById(id).contentDocument || document.frames[id].document;
3 }

如果我們要獲取iframe的window對象,而不是document對象,可以使用document.getElementById(ID).contentWindow的方法。這樣我們就可以使用子窗口中的window對象了,比如子窗口中的函數。

在子窗口中,我們可以通過parent就可以獲得父窗口的window對象,如果假如我們在父窗口有一個函數為getIFrameDOM,我們可以通過parent.getIFrameDOM來調用,同理我們使用parent.document就可以在子窗口中訪問父窗口的document對象了。

父級窗口操作iframe里的dom

JS操作iframe里的dom可是使用contentWindow屬性,contentWindow屬性是指指定的frame或者iframe所在的window對象,在IE中iframe或者frame的contentWindow屬性可以省略,但在Firefox中如果要對iframe對象進行編輯則,必須指定contentWindow屬性,contentWindow屬性支持所有主流瀏覽器。

相關的還有一個contentDocument屬性,這個屬性是指指定的frame或者iframe所在的document對象,但是悲劇的是,ie6-ie7並不支持這個屬性。

ie6和ie7還可以使用document.frames["iframe Name"]或者document.frames["iframe ID"]來獲取相當於contentWindow屬性,而firefox和chrome並不支持這些document.frames["iframe Name"]或者document.frames["iframe ID"],但是window.frames["iframe Name"]或window.frames[index](index是索引值)也支持所有主流瀏覽器。

我們知道document對象是window對象的一個子對象,所以我們可以通過document.getElementById("iframe ID").contentWindow.document來獲取iframe的document對象,相當於contentDocument屬性。

iframe里的js操作父級窗口的dom

iframe里的js要操作父級窗口的dom,必須搞懂幾個對象:

  • parent是父窗口(如果窗口是頂級窗口,那么parent==self==top)。
  • top是最頂級父窗口(有的窗口中套了好幾層frameset或者iframe)。
  • self是當前窗口(等價window)。
  • opener是用open方法打開當前窗口的那個窗口。

這樣iframe里的js要操作父級窗口的dom可以通過parent,top這些對象來獲取父窗口的window對象,例如:

1 parent.document.getElementById("dom ID");

parent,top還能調用父級窗口的的js方法,比如,getIFrameDOM(iID)是父級窗口的一個方法,那么iframe里可以使用parent.getIFrameDOM(“wIframeA”)來調用父級窗口的getIFrameDOM(iID)方法;

雖然iframe在現在WEB開發中越來越少用到了,但是iframe還有很多值得使用的地方,比如使用iframe解決跨域問題.關於iframe還有很多東西要學習,比如iframe自適應高度,使用iframe解決跨域問題,iframe加載問題,iframe加載性能問題等等,還有很多東西要學習。

最后附上一個在指定iframe打開指定頁面的HTML:

01 <div class="refresh" style="margin-right:4em;">
02     <label><input type="radio" name="channel"onclick="window.frames['usermessage-content-frame'].location.href='admin05_0.php';" />待審</label>
03     <label><input type="radio" name="channel"onclick="window.frames['usermessage-content-frame'].location.href='admin05_1.php';" />已審</label>
04     <label><input type="radio" name="channel"onclick="window.frames['usermessage-content-frame'].location.href='admin05_2.php';" />已刪</label>
05     <label><input type="radio" name="channel"onclick="window.frames['usermessage-content-frame'].location.href='admin05.php';" checked="checked" />全部</label>
06 </div>
07 <div class="refresh"><a href="javascript:void(0);"onclick="javascript:refreshContent('usermessage-content-frame');">刷新</a></div>
08  
09 <div id="usermessage-content-div" class="marginTop5 contentIFrame">
10     <iframe id="usermessage-content-frame" name="usermessage-content-frame" frameborder="0" src="page05.php"></iframe>
11 </div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM