<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content=""> <style type="text/css">
#clock{
width:720px;
margin:0 auto;
text-align:left;
margin-top:30px;
}
</style> <script type="text/JavaScript" language="JavaScript">
function $(id){
return document.getElementById(id);
} function displayTime (){
$("clock").innerHTML="<b>"+(new Date()).toLocaleString()+"</b>";
}
function init(){
window.setInterval(displayTime,1000);
} </script> </head> <body onload="init()">
<div id="clock"></div> </body>
</html>
这代码里 window.setInterval(displayTime,1000); 这句,,,也可以写成window.setInterval("displayTime()",1000); 但是不能写成 window.setInterval(displayTime(),1000); 。。想问,JS在把一个函数当参数的时候是不是,要么引号跟括号都有,要么就一号跟括号都没有? 谢谢您拉
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content=""> <style type="text/css">
#clock{
width:720px;
margin:0 auto;
text-align:left;
margin-top:30px;
}
</style> <script type="text/JavaScript" language="JavaScript">
function $(id){
return document.getElementById(id);
} function displayTime (){
$("clock").innerHTML="<b>"+(new Date()).toLocaleString()+"</b>";
}
function init(){
window.setInterval(displayTime,1000);
} </script> </head> <body onload="init()">
<div id="clock"></div> </body>
</html>
这代码里 window.setInterval(displayTime,1000); 这句,,,也可以写成window.setInterval("displayTime()",1000); 但是不能写成 window.setInterval(displayTime(),1000); 。。想问,JS在把一个函数当参数的时候是不是,要么引号跟括号都有,要么就一号跟括号都没有? 谢谢您拉
而displayTime()表示函数的调用,将函数的结果作为参数传递,所以只能是上面的两种写法
而下面的写法【displayTime()】将会把displayTime函数执行的结果传给setInterval作为第一个参数