写了一个时钟的js,发现一个问题, var t=new Date();这一行 必须放在function ch(){}中才能运行,没搞懂这个原理,求解答
<body>
<div class="div1">
<div class="div2">
<span id="y"></span>
<span id="m"></span>
<span id="d"></span>
<span id="w"></span><br />
<span id="h"></span>
<span id="m"></span>
<span id="s"></span>
</div>
</div>
<script type="text/javascript">
var span=document.getElementsByTagName('span')
var week=['日','一','二','三','四','五','六']
function ch(){
var t=new Date();
span[0].innerHTML=t.getFullYear()+'-';
span[1].innerHTML=t.getMonth()+1+'-';
span[2].innerHTML=t.getDate();
span[3].innerHTML=week[t.getDay()];
span[4].innerHTML=t.getHours()+':';
span[5].innerHTML=t.getMinutes()+':';
span[6].innerHTML=t.getSeconds();
}
ch();
setInterval(ch,100);
</script>
千秋此意
相关分类