本帖最后由 mqslll594212 于 2012-07-10 17:44:37 编辑

解决方案 »

  1.   

    在赋值v1的时候dom还未加载,所以document.getElementById("number1")是取不到值的。
      

  2.   

    <html>
    <head>
    <script language="javascript">
    var pub = "全局变量"
    var v1;
    function sayHello(){
    v1 = parseInt(document.getElementById("number1").value);  
    alert("pub是" + pub + ",可以提示出来。");
    }function test(){
    //v1 = parseInt(document.getElementById("number1").value) ;
    alert(v1);
    alert("v1也是全局变量,但是总提示没有定义。");
    }
    </script>
    </head>
    <!-- 在页面载入时执行 -->
    <body onload="sayHello()">
    <input type = 'text' name = 'number1' id = 'number1'><p>
    <input type = 'button' name = 'button' id = 'button' value ='显示' onclick = "test()">
    </body>
    </html>
      

  3.   

    多谢!!<html>
    <head>
    <script language="javascript">
    var pub = "全局变量"
    var v1;
    function sayHello(){
    v1 = parseInt(document.getElementById("number1").value);  
    alert("pub是" + pub + ",可以提示出来。");
    alert(v1);
    }function test(){
    v1 = parseInt(document.getElementById("number1").value) ;
    alert(v1);
    }    
    </script>
    </head>
    <!-- 在页面载入时执行 -->
    <body onload="sayHello()">
    <input type = 'text' name = 'number1' id = 'number1'><p>
    <input type = 'button' name = 'button' id = 'button' value ='显示' onclick = "test()">
    </body>
    </html>
      

  4.   

    看来得好好研究Dom和var赋值了。