服了楼上几位-_-
错误见代码里,我加注释了
另,要3个一起的话建议写成类,生成3个对象就可以了,不需要写三边代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>基于Jquery的一个倒计时的问题(新鲜的逻辑思维)</title>
<script type="text/javascript" src="js/jquery-1.2.6.js"></script>
<style type="text/css">
* {margin:0px;padding:0px;}
html,body {height: 100%;padding:0;margin:0;font-size: 14px;}
body {min-height: 300px;text-align: center;min-width: 402px}
ul {list-style:none;clear: both;}
li {float: left;height:30px;line-height:30px;padding-left:5px;padding-top:5px;}
input {border:1px solid #D1D100;height:20px;line-height:20px;vertical-align:middle;}
.FirstDIV {margin-top: -100px;float: left;width: 100%;height: 30%;}
.SecondDIV {margin: 0px auto;padding:8px;clear: both;overflow: auto;width: 600px;height: 300px;border:1px solid #E3E197;background: #FFD;text-align: left;}
#ViewFrist,#ViewSecond,#ViewThird {background: #FFFF1A;border:1px solid #FD1720;}
</style>
</head>
<body>
<div class="FirstDIV"></div>
<div class="SecondDIV">
<ul>
<li>定义的时间:一 </li><li><input id="FristTime" value="2009-1-15 5:05:20"></li><li id="ViewFrist">这里是倒计时显示区一</li>
</ul>
<ul>
<li>定义的时间:二 </li><li><input id="SecondTime" value="2009-1-14 5:05:20"></li><li id="ViewSecond">这里是倒计时显示区二</li>
</ul>
<ul>
<li>定义的时间:三 </li><li><input id="ThirdTime" value="2009-1-13 5:05:20"></li><li id="ViewThird">这里是倒计时显示区三</li>
</ul>
<ul>
<li>以上在同一个页面自定义了3个时间,用于进行倒计时的这个JS代码呢却只能统计一个时间的倒计时。小弟水平有限,现在产生了2个问题,一:此JS代码在FF下不正常,显示为NaN天NaN时NaN分NaN秒;二:如果要同时进行3个时间的倒计时,就只有写3个同样的JS代码,这样做的话感觉代码冗繁了。想请高手们帮忙看下有没有简单的解决方案。谢谢了!</li>
</ul>
</div>
<script language="javascript">
<!--
//倒计时代码开始
var RemainTime = $("#FristTime").val(); //获取元素为FristTime的input输入框的值(自定义的第一个时间)
var DifferenceHour = -1;
if(RemainTime!='')
{
RemainTime=RemainTime.replace(/-/g,"/");//ff的默认replace是非只替换一次的,所以造成你的RemainTime解析出错
var DifferenceHour = -1;
var DifferenceMinute = -1;
var DifferenceSecond = -1;
var TodayDate = new Date(Date.parse(RemainTime));
var Daysms = 24 * 60 * 60 * 1000;
var Hoursms = 60 * 60 * 1000;
var Secondms = 60 * 1000;
var Microsecond = 1000;
var Account;
Timer();
}
function Timer(){
var Time = new Date();
var Hour = Time.getHours();
var Minute = Time.getMinutes();
var Second = Time.getSeconds();
var TimeValue = ""+((Hour > 12) ? Hour-12:Hour);
TimeValue +=((Minute < 10) ? ":0":":")+Minute;
TimeValue +=((Second < 10) ? ":0":":")+Second;
TimeValue +=((Hour >12 ) ? " PM":" AM");
var convertHour = DifferenceHour; var convertMinute = DifferenceMinute;
var convertSecond = DifferenceSecond;
var Diffms = TodayDate.getTime() - Time.getTime();
DifferenceHour = Math.floor(Diffms / Daysms);
Diffms -= DifferenceHour * Daysms;
DifferenceMinute = Math.floor(Diffms / Hoursms);
Diffms -= DifferenceMinute * Hoursms;
DifferenceSecond = Math.floor(Diffms / Secondms);
Diffms -= DifferenceSecond * Secondms;
var dSecs = Math.floor(Diffms / Microsecond);
Msg = DifferenceHour +"天"+ DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒";
if(DifferenceHour<3){Msg= "<strong class='Red'>"+DifferenceHour +"天"+ DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒</strong>";}
if(DifferenceHour==0){Msg= DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒";}
if(DifferenceMinute==0&&DifferenceHour==0){Msg= DifferenceSecond +"分"+ dSecs+"秒";}
if(DifferenceSecond==0&&DifferenceMinute==0&&DifferenceHour==0){Msg= dSecs+"秒";}
if(DifferenceHour<0){clearTimeout(Account);Msg="<strong class='Red'>交易已经结束</strong>";}
else{Account = setTimeout("Timer()",1000);}
$("#ViewFrist").html(Msg);//返回html内容给元素为ViewFrist的标签
}
//-->
</script>
错误见代码里,我加注释了
另,要3个一起的话建议写成类,生成3个对象就可以了,不需要写三边代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>基于Jquery的一个倒计时的问题(新鲜的逻辑思维)</title>
<script type="text/javascript" src="js/jquery-1.2.6.js"></script>
<style type="text/css">
* {margin:0px;padding:0px;}
html,body {height: 100%;padding:0;margin:0;font-size: 14px;}
body {min-height: 300px;text-align: center;min-width: 402px}
ul {list-style:none;clear: both;}
li {float: left;height:30px;line-height:30px;padding-left:5px;padding-top:5px;}
input {border:1px solid #D1D100;height:20px;line-height:20px;vertical-align:middle;}
.FirstDIV {margin-top: -100px;float: left;width: 100%;height: 30%;}
.SecondDIV {margin: 0px auto;padding:8px;clear: both;overflow: auto;width: 600px;height: 300px;border:1px solid #E3E197;background: #FFD;text-align: left;}
#ViewFrist,#ViewSecond,#ViewThird {background: #FFFF1A;border:1px solid #FD1720;}
</style>
</head>
<body>
<div class="FirstDIV"></div>
<div class="SecondDIV">
<ul>
<li>定义的时间:一 </li><li><input id="FristTime" value="2009-1-15 5:05:20"></li><li id="ViewFrist">这里是倒计时显示区一</li>
</ul>
<ul>
<li>定义的时间:二 </li><li><input id="SecondTime" value="2009-1-14 5:05:20"></li><li id="ViewSecond">这里是倒计时显示区二</li>
</ul>
<ul>
<li>定义的时间:三 </li><li><input id="ThirdTime" value="2009-1-13 5:05:20"></li><li id="ViewThird">这里是倒计时显示区三</li>
</ul>
<ul>
<li>以上在同一个页面自定义了3个时间,用于进行倒计时的这个JS代码呢却只能统计一个时间的倒计时。小弟水平有限,现在产生了2个问题,一:此JS代码在FF下不正常,显示为NaN天NaN时NaN分NaN秒;二:如果要同时进行3个时间的倒计时,就只有写3个同样的JS代码,这样做的话感觉代码冗繁了。想请高手们帮忙看下有没有简单的解决方案。谢谢了!</li>
</ul>
</div>
<script language="javascript">
<!--
//倒计时代码开始
var RemainTime = $("#FristTime").val(); //获取元素为FristTime的input输入框的值(自定义的第一个时间)
var DifferenceHour = -1;
if(RemainTime!='')
{
RemainTime=RemainTime.replace(/-/g,"/");//ff的默认replace是非只替换一次的,所以造成你的RemainTime解析出错
var DifferenceHour = -1;
var DifferenceMinute = -1;
var DifferenceSecond = -1;
var TodayDate = new Date(Date.parse(RemainTime));
var Daysms = 24 * 60 * 60 * 1000;
var Hoursms = 60 * 60 * 1000;
var Secondms = 60 * 1000;
var Microsecond = 1000;
var Account;
Timer();
}
function Timer(){
var Time = new Date();
var Hour = Time.getHours();
var Minute = Time.getMinutes();
var Second = Time.getSeconds();
var TimeValue = ""+((Hour > 12) ? Hour-12:Hour);
TimeValue +=((Minute < 10) ? ":0":":")+Minute;
TimeValue +=((Second < 10) ? ":0":":")+Second;
TimeValue +=((Hour >12 ) ? " PM":" AM");
var convertHour = DifferenceHour; var convertMinute = DifferenceMinute;
var convertSecond = DifferenceSecond;
var Diffms = TodayDate.getTime() - Time.getTime();
DifferenceHour = Math.floor(Diffms / Daysms);
Diffms -= DifferenceHour * Daysms;
DifferenceMinute = Math.floor(Diffms / Hoursms);
Diffms -= DifferenceMinute * Hoursms;
DifferenceSecond = Math.floor(Diffms / Secondms);
Diffms -= DifferenceSecond * Secondms;
var dSecs = Math.floor(Diffms / Microsecond);
Msg = DifferenceHour +"天"+ DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒";
if(DifferenceHour<3){Msg= "<strong class='Red'>"+DifferenceHour +"天"+ DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒</strong>";}
if(DifferenceHour==0){Msg= DifferenceMinute +"小时"+ DifferenceSecond +"分"+ dSecs+"秒";}
if(DifferenceMinute==0&&DifferenceHour==0){Msg= DifferenceSecond +"分"+ dSecs+"秒";}
if(DifferenceSecond==0&&DifferenceMinute==0&&DifferenceHour==0){Msg= dSecs+"秒";}
if(DifferenceHour<0){clearTimeout(Account);Msg="<strong class='Red'>交易已经结束</strong>";}
else{Account = setTimeout("Timer()",1000);}
$("#ViewFrist").html(Msg);//返回html内容给元素为ViewFrist的标签
}
//-->
</script>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货