通过id获取元素计时器的数字有变化,通过元素名获取元素的计时器的数字没有变化!(看注释)
<!DOCTYPE html>
<html>
<head>
<title>浏览器对象</title>
<meta http-equiv="Content-Type" content="text/html; charset=gkb"/>
<style>
.box{background-color: #efefef; padding: 10px;}
.box span{font-weight: bold;}
.box a{color: #008;}
</style>
</head>
<body>
<div class="box">
<p>操作成功</p>
<p><span id="second">5</span>秒后回到主页 <a href="javascript:void()" alt="回到上一页面" onclick="goBack()">返回</a></p>
</div>
<script type="text/javascript">
var time = 5;
var timer = setInterval(function(){
document.getElementById("second").innerHTML = time; //时间会变化
//这里我改为 document.getElementsByTagName("span").innerHTML = time; 它的时间就不变化,是为什么啊?
time--;
if(time==0){
window.location.href = "http://www.imooc.com/";
}
},1000)
function goBack(){
window.history.back();
}
</script>
</body>
</html>