JavaScript對iframe的DOM操做

在IE六、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:javascript

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

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

在子窗口中,咱們能夠經過parent就能夠得到父窗口的window對象,若是假如咱們在父窗口有一個函數爲getIFrameDOM,咱們能夠經過parent.getIFrameDOM來調用,同理咱們使用parent.document就能夠在子窗口中訪問父窗口的document對象了。java

父級窗口操做iframe裏的dom

JS操做iframe裏的dom但是使用contentWindow屬性,contentWindow屬性是指指定的frame或者iframe所在的window對象,在IE中iframe或者frame的contentWindow屬性能夠省略,但在Firefox中若是要對iframe對象進行編輯則,必須指定contentWindow屬性,contentWindow屬性支持全部主流瀏覽器。chrome

相關的還有一個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屬性。dom

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>
相關文章
相關標籤/搜索