我想在js中动态更改iframe中的页面,用原以为理所当然的方法去实现,结果几种方法却大相径庭,不得其解,请高手指点:页面(Main.aspx)中有一个iframe:
<iframe id="mainFrame" frameborder="0" width="100%" height="554px;" scrolling="auto" src='MyDesktop.aspx'></iframe>Main.aspx中引用了一个外部js文件。
1、我先用如下方法:
   document.getElementById("mainFrame").src = ...;
   结果ok2、然后用如下方法:
  document.getElementById("mainFrame").document.location.href = ...;
  结果出错
  调试:alert(document.getElementById("mainFrame").document.location.href),没有出现我所期望的MyDesktop.aspx,而是Main.aspx!3、最后用如下方法:
   var mainFrame = top["mainFrame"];
   mainFrame.document.location.href = ...;
   结果ok
   调试:alert(mainFrame.document.location.href),得到的是MyDesktop.aspx疑问:在方法2和方法3中,用document.getElementById("mainFrame")和用top["mainFrame"]得到的对象为什么不一样???

解决方案 »

  1.   

    根据结果看,top["mainFrame"]和document.getElementById("mainFrame")所指向的对象是不一致。
    我的疑问是:document.getElementById("mainFrame")肯定指向iframe,那么top["mainFrame"]又指向什么呢?js中top到底有何用法?
      

  2.   

    document.getElementById("mainFrame")获得的是parent页中的iframe的dom对象 所以可以通过src改变 获得不了子页的document
     
    top["mainFrame"]其实获得的是iframe中的子页对象 可以获得子页的document
      

  3.   

    document.getElementById("mainFrame")得到的是DOM对象,而top["mainFrame"]得到的是BOM对象?