通過id獲取元素計時器的數(shù)字有變化,通過元素名獲取元素的計時器的數(shù)字沒有變化?。醋⑨專?/h1>
<!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; ?它的時間就不變化,是為什么?。?/p>
? ? ? ? time--;
? ? ? ? if(time==0){
? ? ? ? ? ? window.location.href = "http://idcbgp.cn/";
? ? ? ? }
? ? },1000)?
? ?function goBack(){
? ? window.history.back(); ??
? ? }
?</script> ?
</body>
</html>
<!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; ?它的時間就不變化,是為什么?。?/p>
? ? ? ? time--;
? ? ? ? if(time==0){
? ? ? ? ? ? window.location.href = "http://idcbgp.cn/";
? ? ? ? }
? ? },1000)?
? ?function goBack(){
? ? window.history.back(); ??
? ? }
?</script> ?
</body>
</html>
2016-02-16
.getElementsByTagName返回的是一個集合,
你要用的話就?document.getElementsByTagName("span")[0].innerHTML = time