你想在function中再套个function?

解决方案 »

  1.   

    document.getElementById("container").innerHTML = temp; 
    你这里的("container")  应该是单引号(‘container’)  
      

  2.   


    <body onload="test();"> 
    <font> <h3 align="center">试试javascript </h3> </font> 
    <br>      <div id="container" >Weitting... </div> <script type="text/Javascript"> var temp=""; 
    function test() 
    {  (function() 

    temp = temp +"ok."; 
    document.getElementById("container").innerHTML = temp; 
    window.setTimeout("test();",1000); 
    })()} </script> </body>
      

  3.   

    因为test的作用域仅限于test1,在test1之外无法访问
    function f1(){
        function f2(){
            alert("f2");    
        }
        f2();
    }
    //f2();这样用会报错
    f1();
      

  4.   

    惭愧!我的主贴中有点小问题,重写下。这个可以运行:<body>
    <font> <h3 align="center">试试javascript </h3> </font> 
    <br>      <div id="container" >Weitting... </div> <script type="text/Javascript"> var temp=""; function test1() 
        { 
    temp = temp +"ok."; 
    document.getElementById("container").innerHTML = temp; 
        window.setTimeout("test1();",1000); 
        }
     
    test1();</script> </body>而改成这个不能运行:<body> 
    <font> <h3 align="center">试试javascript </h3> </font> 
    <br>      <div id="container" >Weitting... </div> <script type="text/Javascript"> function test() 
    { var temp=""; function test1() 
        { 
    temp = temp +"ok."; 
    document.getElementById("container").innerHTML = temp; 
        window.setTimeout("test1();",1000); 
        } test1();} test();</script> </body>
      

  5.   


    <body> 
    <font> <h3 align="center">试试javascript </h3> </font> 
    <br>      <div id="container" >Weitting... </div> <script type="text/Javascript"> 
    var temp=""; 
    function test() 
    { function test1() 
        { 
    temp = temp +"ok."; 
    document.getElementById("container").innerHTML = temp; 
        window.setTimeout("test()",1000); ////
        } test1(); } test(); </script> </body> 
      

  6.   


    按你说的是对的,但把你这个其中的alert("f2")换为我上面的内容似乎不行:function f1(){     var temp="";
     
        function f2(){ 
        temp = temp +"ok."; 
        document.getElementById("container").innerHTML = temp; 
        window.setTimeout("f2();",1000);     } 
        f2(); 
    } f1();
      

  7.   


    OK!这个可以运行!
    但,window.setTimeout("test()",1000); 中间的test()为什么不能使用test1()呢?
      

  8.   

    函数作用域问题 因为window.setTimeout("test1()",1000)运行是还是要从最外层函数读取test1函数,不能够直接读取到
    所以只能通过调用外层test来调用test1()来调用
      

  9.   

    window.setTimeout("<函数名>",<nTime>)这种写法只能调用显示声明的全局函数,调用内部函数可以使用
    局部变量来存储函数闭包
    function f1(){
        var f0=function f2(){
            alert("f2");   
        }
        window.setTimeout(f0,1000);
    }
    f1();