已经取得
 function getDoc(){
var doc = document.body.getElementsByTagName("iframe");
for(var i=0;i<doc.length;i++){

      if(doc[i].src.indexOf('fuyou001')>-1){
   if (document.all){
              return   doc = doc[i].document;
        }else{   
               return  doc = doc[i].contentDocument;
        }

  };
    } }
var obj = getDoc();
为什么obj.getElementById('mail')返回是null 但明明有mail这个id的

解决方案 »

  1.   

    return doc[i].contentWindow.document;
      

  2.   

    调用的时候iframe的内容是否加载完毕? 最好写在函数里,由iframe的onload来回调function getDoc(){ 
      var doc = document.body.getElementsByTagName("iframe"); 
      for(var i=0;i <doc.length;i++){ 
        if(doc[i].src.indexOf('fuyou001')>-1){ 
          return  doc = doc[i].contentWindow.document; 
        } 
      }
    }
    var obj; 
    window.onload(){
      obj = getDoc(); 
      alert(obj.getElementById('mail'));
    }
      

  3.   

    因为你的方法里没有return这个framevar obj=getDoc()并没有得到这个frame;obj.getElementById('mail')自然得不到
      

  4.   

    补充说明下,是有可能还没return到,也就是加载没完成
      

  5.   


    firfox 下报
    <file://> à³CPΠ<http://hud.zhulang.com> ûÖ^' Window.document
    getDoc()union_ad.php.htm (第 165 行)
    pre()union_ad.php.htm (第 136 行)
    [Break on this error] return doc = doc[i].contentWindow.document; \r\n
    还有上面那个在ie下能用吗
      

  6.   

    alert(obj); / / 打出object ;
    应该返回来了,才对啊
      

  7.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
    </head> 
    <body onload="aa()"> 
    <iframe name="aaaaa" src='test.html' ></iframe>
    <script language="javascript" type="text/javascript"> 
    function getDoc(){ 
    var doc = document.frames ;
    for(var i=0;i <doc.length;i++){ 
       if(document.all){
             return doc[i].window.document; 
            }else{  
              return doc[i].contentDocument; 
            }  
        } 

    function aa(){
      obj = getDoc(); 
      alert(obj.getElementById('mail').value);
    }</script>
    </body> 
    </html> 
      

  8.   

    如果你的iframe是跨域调用的话就不行了。
      

  9.   


    这个在ie下会报错Error :缺少对象
      

  10.   

    Line:13
    Char:13
    Code:0
    Error:'window.document' 为空或不是对象
      

  11.   

    以下代码ie,ff下测试都可以:<script> 
    function getDoc(){ 
      var doc = document.body.getElementsByTagName("iframe"); 
      for(var i=0;i <doc.length;i++){ 
        if(doc[i].src.indexOf('test1')>-1){ 
          return  doc = doc[i].contentWindow.document; 
        } 
      }
    }
    var obj; 
    window.onload=function(){
      obj = getDoc(); 
      alert(obj.getElementById("mail").innerHTML);
    }
    </script>
    <iframe src="test1.asp?x"></iframe>
    test1.asp<div id="mail">xxaxxx</div>
      

  12.   

    http://hud.zhulang.com/game/union_ad.php?class=7
    就上面的网址,哪位能取出id 为mail 的object 
      

  13.   

    是用的我的源码么
    我这里是var doc = document.frames ;获得的frame集合
    不是 你上面的var doc = document.body.getElementsByTagName("iframe"); 
    用 你的获得frame集合地方法 就报 'window.document' 为空或不是对象
      

  14.   

    我现在要取出id 为email 的object 
    有什么办法可以取 这个贴子都是为了取出id 为email 的object 对象 的发的
      

  15.   

    else{ 
                  return  doc = doc[i].contentWindow.document;      //FF中使用