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>
相关文章
相关标签/搜索