例如a.js里有    function divclose()
    {
        document.getElementById("ad").style.display="none";
    }b.js里有<div id="ad" style="position:absolute; right:0; bottom:0;">我怎么样在a.js里取得b中的div的id "ad"呢?

解决方案 »

  1.   

    a和b是什么关系呢?
    PS:b.js里怎么还有html标签的呀
      

  2.   

    那个div是用js创建的动态div吧?
      

  3.   

    a 和b 是两个单独的js文件
      

  4.   

    function divclose(divid)
    {
            document.getElementById(divid).style.display="none";
    }
    很明显,你要把函数修改成如上的样子,
    在需要隐藏的地方调用divclose
      

  5.   

    我觉得 lz的方法不可能成功  
    第一 js文件中 出现 <div>这样的html标签 不大可能吧
    第二 就算你的html导入这两个js文件 document 对象是否一致呢(个人的疑问)
      

  6.   

    应该是楼主的JS文件顺序不对吧
    main.html要保证添加div代码在body生成后<!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>
      <title> new document </title> 
     </head>
     <body>  
     </body>
     <script src="b.js" type="text/javascript"></script>
     <!-- 
     <script src="a.js" type="text/javascript"></script>
     -->
    </html>
    现在是现实div把注释拿去可以看见a.js起作用b.jsvar odiv = document.createElement("div");
    odiv.id = "ad";
    odiv.style.width = "40px";
    odiv.style.height = "40px";
    odiv.style.backgroundColor = "red";
    document.body.appendChild(odiv);a.js    function divclose()
        {
            document.getElementById("ad").style.display="none";
        }
    divclose()