可以运行的代码放到另一个函数里就不能运行了。 你想在function中再套个function? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 document.getElementById("container").innerHTML = temp; 你这里的("container") 应该是单引号(‘container’) <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> 因为test的作用域仅限于test1,在test1之外无法访问function f1(){ function f2(){ alert("f2"); } f2();}//f2();这样用会报错f1(); 惭愧!我的主贴中有点小问题,重写下。这个可以运行:<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> <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> 按你说的是对的,但把你这个其中的alert("f2")换为我上面的内容似乎不行:function f1(){ var temp=""; function f2(){ temp = temp +"ok."; document.getElementById("container").innerHTML = temp; window.setTimeout("f2();",1000); } f2(); } f1(); OK!这个可以运行!但,window.setTimeout("test()",1000); 中间的test()为什么不能使用test1()呢? 函数作用域问题 因为window.setTimeout("test1()",1000)运行是还是要从最外层函数读取test1函数,不能够直接读取到所以只能通过调用外层test来调用test1()来调用 window.setTimeout("<函数名>",<nTime>)这种写法只能调用显示声明的全局函数,调用内部函数可以使用局部变量来存储函数闭包function f1(){ var f0=function f2(){ alert("f2"); } window.setTimeout(f0,1000);}f1(); fckeditor中如何嵌入WebEq 找个漂亮点的页面~~给女生看的!! js表单验证获取光标问题 你怎么看待web前端开发工程师这一行业的? 又来::msxml3.dll 错误 '800c0005'问题 js 正则表达式 号码+古话等 如何解决open一个窗口导致session丢失? 请问什么版本的哪些浏览器支持designMode和contentEditable属性? 在线等候,请问如何去除回车符与空格?? 如何在进入页面时,焦点定位在<input type="text"> 输入框中呢! 那位高手可以仿造这个好菜单? 关闭窗口的问题,急啊!
你这里的("container") 应该是单引号(‘container’)
<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>
function f1(){
function f2(){
alert("f2");
}
f2();
}
//f2();这样用会报错
f1();
<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>
<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>
按你说的是对的,但把你这个其中的alert("f2")换为我上面的内容似乎不行:function f1(){ var temp="";
function f2(){
temp = temp +"ok.";
document.getElementById("container").innerHTML = temp;
window.setTimeout("f2();",1000); }
f2();
} f1();
OK!这个可以运行!
但,window.setTimeout("test()",1000); 中间的test()为什么不能使用test1()呢?
所以只能通过调用外层test来调用test1()来调用
局部变量来存储函数闭包
function f1(){
var f0=function f2(){
alert("f2");
}
window.setTimeout(f0,1000);
}
f1();