例如,申明局部变量可以这样:
var x;if(x==undefined){....}现在要申明一个全局变量但不赋值该怎么写呢?

解决方案 »

  1.   

    写在方法的外面就是全局的啊,
    var x;
    其他地方都可以使用x
      

  2.   

    是想达到这样的效果,点击DIV时改变URL的值,该怎么弄?<head><script>window.onload=function(){w()}</script></head>
    <script>(function www(){
    var url="1.jpg";
    alert(url);
    w=function(){document.getElementById("aaa").onclick=function (){url="a.jpg";return www();}
                  document.getElementById("bbb").onclick=function (){url="b.jpg";return www(); }  
    }
    })()</script><div id="aaa" style="width:100px;height:100px;background:#ff0000"></div>
    <div id="bbb" style="width:100px;height:100px;background:#ffff00"></div>
      

  3.   

    <head><script>window.onload=function(){w()}</script></head>
    <script>
    var url="1.jpg";
    function www(){
    alert(url);
    }
    w=function(){document.getElementById("aaa").onclick=function (){url="a.jpg";return www();}
                  document.getElementById("bbb").onclick=function (){url="b.jpg";return www(); }  
    }
    </script><div id="aaa" style="width:100px;height:100px;background:#ff0000"></div>
    <div id="bbb" style="width:100px;height:100px;background:#ffff00"></div>
      

  4.   

    这个包围函数(function www(...){})()里页还有太多内容不好改,若是把var url="1.jpg"放到外面;加载页面时就会运行
    (function www(...){})()但就无法获取var url的值就会出错。