87,992
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>显示当前时间</title>
</head>
<body>
<style type="text/css">
#d1{
font-style: oblique;
font-size: larger;
background: red;
color: blue;
}
</style>
<script type="text/javascript">
//两个变量放入showTime函数体内就正确,为什么呀?
var now=new Date();
var div=document.getElementById("d1");
div.innerHTML=now.toLocaleString();
function showTime(){
div.innerHTML=now.toLocaleString();
}
function starTime(){
v = window.setInterval("showTime()", 1000);
}
function stopTime(){
window.clearInterval(v);
}
</script>
</body>
<div id="d1">当前时间</div>
<input type="button" name="button" id="btn1" value="显示时间" onclick="starTime()"/>
<input type="button" name="button" id="btn2" value="停止时间" onclick="stopTime()"/>
</html>
求大佬解答,两个var定义的变量放入showtime函数就正确了,为什么呀,var定义不是局部变量吗,在<script>中都有生效。
明白了,我去百度研究一下作用域