var test=1;1.在页面中直接写出来,可以alert(test)出来,
2.保存在外部1.js文件中,在页面中进行引用也可以alert(test)出来3.
用a=document.createElement('script');
a.setAttribute("type","text/javascript");
a.src="1.js";   //1.js即外部JS
document.body.appendChild(a);这种方式就不能调用test了
向大家寻求一下问题所在。谢谢你们了。

解决方案 »

  1.   

    注意,1.js 与 1.html 要放在同一目录下!L@[email protected]
    var test=1;
    alert(test);1.html
    <!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>
      <meta name="generator" content="editplus" />
      <meta name="author" content="" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    var a=document.createElement('script');
    a.setAttribute("type","text/javascript");
    a.src="1.js";  //1.js即外部JS
    document.body.appendChild(a);
      //-->
      </script>
     </body>
    </html>
      

  2.   

    恩,可能我没表述清楚,alert是在html里面的,不是js文件里,
    简单的说就是htm访问不到createElement('script')方式声明的变量。还是感谢您的回复
      

  3.   

    比如这样:
    <html>
    <head>
    </head>
    <body>
    <script>
    function f1()
    {
    var a=document.createElement('script');
    a.setAttribute("type","text/javascript");
    a.src="1.js";
    document.body.appendChild(a);
    alert(test);
    }
    f1();
    </script>
    </body>
    </html>直接写的<script src="1.js"></script><script>alert(test);</script>
    是没问题的
      

  4.   

    客户端JavaScript对JavaScript代码的解释执行是有顺序的,页面上的代码顺序优先级最高,外部引入的最低
      

  5.   

    呵呵,为 script 增加延迟执行标记 defer!L@[email protected]
    var test=1;
    1.html
    <!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>
      <meta name="generator" content="editplus" />
      <meta name="author" content="" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript" defer>
      <!--
    var a=document.createElement('script');
    a.setAttribute("type","text/javascript");
    a.src="1.js";  //1.js即外部JS
    document.body.appendChild(a);alert(test);
      //-->
      </script>
     </body>
    </html>
      

  6.   

    Web 开发常用手册JScript语言参考.rar
    http://download.csdn.net/source/308916DHTML参考手册.rar —— DEFER Attribute | defer Property
    http://download.csdn.net/source/308913样式表中文手册.chm
    http://download.csdn.net/source/304124
      

  7.   

    TO WWY_0918:外部引入,如果直接写<script src="1.js">是可以的,而document.createElement('script')不行,可能是顺序问题,思考解决办法ing。
      

  8.   

    TO:yixianggao谢谢你,问题解决了。:)
      

  9.   


    像这种方式中,在调用函数f1的时候,变量test在函数的作用域内即当前产生的调用对象中是不存在的。而直接写,alert函数和test变量就都在全局作用域内是可以引用的。
      

  10.   

    谢谢wwy_0918,可惜结贴了,有机会补你分。谢谢谢谢
      

  11.   

    [Quote=引用 9 楼 wwy_0918 的回复:]<html> 
    <head> </head> 
    <body> 
    <script>
    function f1()
    {var a=document.createElement('script');
    a.setAttribute("type","text/javascript");
    a.src="1.js";
    document.body.appendChild(a);
    alert(test);
    }
    f1(); //变量test在函数的作用域内即当前产生的调用对象中是不存在的
    </script> 
    </body> 
    </html>直接写的 <scriptsrc="1.js"> </script> <script>alert(test); </script>
    是没问题的//而直接写,alert函数和test变量就都在全局作用域内是可以引用的。----变量作用域的问题,我第一次如此明白的理解了,注意到了。谢谢