TestIframe.html<html>
<head>
<script type="text/javascript">
alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
function f(){
        var doc;        if (document.all){//IE
                doc = document.frames["MyIFrame"].document;
        }else{//Firefox
                doc = document.getElementById("MyIFrame").contentDocument;
        }
        doc.getElementById("s").innerHTML += "123456789";
}
</script>
</head>
<body onload="f()"><iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500"></body>
</html>MyIFrame.html<h1 id = "s" style="color:red;" >内容<h1>
firefox 提示document.getElementById("MyIFrame") is null

解决方案 »

  1.   

    执行
    alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
    的时候,iframe还不存在
    你把这段script放在
    <iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500">
    的下面就行了
      

  2.   

    window.top
    window.left
    window.right
    window.parent
    通过他们
      

  3.   

    <script type="text/javascript">
    //alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
    function f(){
      alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
      var doc;  if (document.all){//IE
      doc = document.frames["MyIFrame"].document;
      }else{//Firefox
      doc = document.getElementById("MyIFrame").contentDocument;
      }
      doc.getElementById("s").innerHTML += "123456789";
    }
    </script>
      

  4.   

    <iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500">
    <script type="text/javascript">
    alert("123");
    alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
    doc.getElementById("s").innerHTML += "123456789";</script>这样的效果是 报错也不执行 啊  搞了几天了 有点晕了  我想动态操作这里面的内容
      

  5.   


    <iframe id = "MyIFrame" name = "MyIFrame" src = "MyIFrame.html" width = "500" height="500">
    <script type="text/javascript">
    alert("123");
    alert(document.getElementById("MyIFrame").contentWindow.document.body.innerHTML);
    doc.getElementById("s").innerHTML += "123456789";</script>
    刚说错了 
    这样的效果是 不报错也不执行 啊 搞了几天了 有点晕了 我想动态操作这里面的内容
      

  6.   

    doc是什么?
    试试这样:
    document.frames['MyIFrame'].document.getElementById("s").innerHTML += "123456789";
      

  7.   


    用document.onkeydown 事件可以操作了 谢谢各位