好久没在js区发贴了,今天散点分。
我发现document.getElementById('myframe')和window.frames[i]还不一样。看例子[ie6测试环境]:<html>
  <head>
  <title>differences</title>
  </head>
  <body>
      <iframe id="myframe" src="http://www.baidu.com" height=400 width=400></iframe>
  </body>
  <script type="text/javascript">
document.body.onload = function(){
//这种写法没问题
//document.getElementById('myframe').src = 'http://www.google.com';
//这种写法会出错
//document.getElementById('myframe').location.href='http://www.google.com';
//这种写法可行
//window.frames[0].location.href='http://www.google.com';
//这种会有permission denied错误。
//window.frames[0].src='http://www.google.com';
}
</script>
</html>

解决方案 »

  1.   

    document.getElementById('myframe').contentWindow.location.href = 'http://www.google.com';
      

  2.   

    高!
    但是contentWindow是哪个对象或组件的属性呢?
      

  3.   

    好像是window与document的区别,放哪都一样
      

  4.   

    <body>
          <iframe id="myframe" src="" height=400 width=400></iframe>
      </body>
      <script type="text/javascript">
            window.onload = function(){
            //这种写法没问题
            //document.getElementById('myframe').src = 'http://www.google.com';
            //这种写法会出错
            //document.getElementById('myframe').location.href='http://www.google.com';
            //这种写法可行
            //window.frames[0].location.href='http://www.google.com';
            //这种会有permission denied错误。
            //window.frames[0].src='http://www.google.com';
    //这样看看什么不同
    alert(typeof window.frames[0].tagName)
    alert(document.getElementById('myframe').tagName)
        }
        </script>
      

  5.   

    contentWindow是框架的,相当于页面的window
      

  6.   


    如果只想改变iframe的 src 或者 border ,scrolling 等attributes(与property不是一个概念,property是不能写在标签内的,比如:scrollHeight,nnerHTML等),就需要用到第一种方法。如果想取得iframe的页面(不是iframe本身)。就需要使用第二种方法,因为它取得的是一个完整的DOM模型,比如想得到iframe的document.body的内容,就只能用第二种方法另:
    document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:在IE为是frames[id].document或document.getElementById(id).contentWindow.document; 
    在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument;
      

  7.   


    var obj = document.getElementById(“iframe”);//获取对象  
    3.var dom = document.all.frames[“iframe”];//获取DOM
    如果只想改变iframe的 src 或者 border ,scrolling 等attributes(与property不是一个概念
    ,property是不能写在标签内的,比如:scrollHeight,nnerHTML等),就需要用到第一种方法。如果想取得iframe的页面(不是iframe本身)。就需要使用第二种方法,因为它取得的是一个完整的DOM模型,
    比如想得到iframe的document.body的内容,就只能用第二种方法另:
    document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:在IE为是frames[id].document或document.getElementById(id).contentWindow.document; 
    在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument;
      

  8.   


    其实主要你搞清楚 你要操作的是iframe对象本身(比如src是iframe的属性)
    还是iframe对象里面的DOM元素就可以了(比如location是iframe里面DOM的window的属性)
    var obj = document.getElementById(“iframe”);//获取对象  
    var dom = document.all.frames[“iframe”];//获取DOM
    然后就卡这两条.一个是对象 一个是DOM.
    ======================
    最后记住这两个语法格式 相信操作就没问题了
    document.getElementById取到的iframe是不能直接操作里面的document的,只能这样取:在IE为是frames[id].document或document.getElementById(id).contentWindow.document; 在firefox为frames[name].contentDocument或document.getElementById(id).contentDocument; 
      

  9.   

    请高手解释一些,iframe自适应写法,虽然我是这样写的,但无法解释。
    方法一:
    <iframe id="content" name="content" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" onload="this.document?this.style.height=content.document.body.scrollHeight+5:this.height=this.contentDocument.body.scrollHeight+5;">
    </iframe>注意:
    this.style.height=content.document.body.scrollHeight+5 //支持IE
    this.height=this.contentDocument.body.scrollHeight+5 //支持FF
      

  10.   

    做Iframe高度自适应的时候,一般用window.frames['frame'].getXXX()调用子页面JS获取高度,然后设置 document.getElementById('frame').style.height