发表于:2008-03-08 16:32:20 楼主 
HTML code
<html>
<head>
<script type="text/javascript">    show_on = function(){
        var myinput = document.getElementById("myinput");
        var ifrm = document.getElementById("hdnfrm");
        ifrm = ifrm.contentWindow.document;
        
        var ifrm_body = ifrm.body;
        
        var ofile = ifrm.createElement("input");
        ifrm_body.appendChild(ofile);    }window.onload = show_on;
</script>
</head>
<body>
<span id="myinput"><iframe id="hdnfrm" style="width:283px;height:230px;" scrolling="no" frameborder="1" ></iframe></span>
</body>
</html>以上代码无问题,能够在firefox页面里面的iframe里正常添加个input HTML code
<html>
<head>
<script type="text/javascript">    show_on = function(){
        var myinput = document.getElementById("myinput");
        
        var ifrm = document.createElement("iframe");
        ifrm.setAttribute("style","width:283px;height:230px;");
        ifrm.setAttribute("scrolling","no");
        ifrm.setAttribute("frameborder","1");
        myinput.appendChild(ifrm);
        ifrm = ifrm.contentWindow.document;
        
        var ifrm_body = ifrm.body;
        alert(ifrm);//看看是否为对象
        var ofile = ifrm.createElement("input");
        t=setTimeout(function(){ifrm_body.appendChild(ofile);},2000)//试试过两秒看行不行.
    }window.onload = show_on;
</script>
</head>
<body>
<span id="myinput"></span>
</body>
</html>
 
 

解决方案 »

  1.   


    HTML code 
    <html> 
    <head> 
    <script type="text/javascript">     show_on = function(){ 
            var myinput = document.getElementById("myinput"); 
             
            var ifrm = document.createElement("iframe"); 
            ifrm.setAttribute("style","width:283px;height:230px;"); 
            ifrm.setAttribute("scrolling","no"); 
            ifrm.setAttribute("frameborder","1"); 
            myinput.appendChild(ifrm); 
            ifrm = ifrm.contentWindow.document; 
             
            var ifrm_body = ifrm.body; 
            alert(ifrm);//看看是否为对象 
            var ofile = ifrm.createElement("input"); 
            t=setTimeout(function(){ifrm_body.appendChild(ofile);},2000)//试试过两秒看行不行. 
        } window.onload = show_on; 
    </script> 
    </head> 
    <body> 
    <span id="myinput"> </span> 
    </body> 
    </html> 经过测试,它肯定是对象,经过2秒后再试也不行
      

  2.   

    不错的方法,input实际上已经添加成功了,但是不知为什么,添加完后又自己消失了,难道是“生命已经结束”?也没搞明白……
      

  3.   


    对啊!我用innerHTML是能看到已经添加成功的!!
    就是没显示..难道是BUG吗?
      

  4.   

    <!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN">   
      <HTML>   
      <HEAD>   
      <TITLE>   New   Document   </TITLE>   
      <META   NAME="Generator"   CONTENT="EditPlus">   
      <META   NAME="Author"   CONTENT="">   
      <META   NAME="Keywords"   CONTENT="">   
      <META   NAME="Description"   CONTENT="">   
      </HEAD>   
        
      <BODY>   
          <select   name="listbox1"   size="4"   multiple>   
              <option   value="sdgs">sdg</option>   
              <option   value="sg">sdg</option>   
              <option   value="sdg">sdg</option>   
          </select>   
      </BODY>   
      </HTML>   
      <SCRIPT   LANGUAGE="JavaScript">   
      <!--   
      var   frm   =   document.createElement("iframe");   
      document.body.appendChild(frm);   
      var   ifram   =   document.frames[0];   
      ifram.document.write("这是动态生成的<br>");   
      frm.document.write("这是动态生成的");   
        
      var   listbox   =   document.all("listbox1")   
      for(var   i=0;i<listbox.length;i++)   
      {   
      ifram.document.write(listbox[i].value   +   "<br>");   
      }   
        
        
      //-->   
      </SCRIPT>这个例子你能看明白吗
      

  5.   


    谢谢你的回答,但是你的代码一样..在IE中正常..在FF中依然没反应...
      

  6.   

    关注中,为什么IFRAME中的BODY为空吗?期待高手解答...顶...
      

  7.   


    <html>
    <head>
    <script type="text/javascript">    show_on = function(){
             myinput = document.getElementById("myinput");
            ifrm = document.createElement("iframe");
            ifrm.setAttribute("style","width:283px;height:230px;");
            ifrm.setAttribute("scrolling","no");
            myinput.appendChild(ifrm);
            ifrm_d = ifrm.contentWindow.document;
    var ifrm_body = ifrm_d.body;
            var ofile = ifrm_d.createElement("input");
    ofile.value="asdf";
            ifrm_body.appendChild(ofile);

    setTimeout(tt,"20ms");    }

    function tt(){
            ifrm_d = ifrm.contentWindow.document;
          
    var ifrm_body = ifrm_d.body;
            var ofile = ifrm_d.createElement("input");
            ifrm_body.appendChild(ofile);
    }window.onload = show_on;
    </script>
    </head>
    <body>
    <div id="myinput"></span>
    <input type="button" id="sd" onClick="tt()">
    </body>
    </html>这是可行的。
      

  8.   

    setTimeout(tt,0);也能成功..
    曾经在FF的官方网站看的某篇E文文章好像有说这样可以解决一些BUG..
    晕..奇怪...不过谢谢
    yangbo99 
      

  9.   


    哦,原来是这样啊,谢谢!
    不过,在IE(6.0)下好像又通不过了,
    var ifrm_body = ifrm_d.body;
    IE不认识body属性
    …………
      

  10.   

    IE下,直接删掉body属性,
    var ifrm_body = ifrm_d;
    测试通过,不知对不对