看不懂LZ的JS语法,更看不懂LZ的代码逻辑。大概理解了你的功能,下面代码可以实现:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <meta http-equiv=Content-Type content="text/html;charset=utf-8"> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT="Philoo"> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <SCRIPT LANGUAGE="JavaScript"> <!-- setInterval('displaytime()', 1000); function displaytime() { var strTime = document.getElementById("txt1").value;// var hh = parseInt(strTime.substr(0,2), 10); var mm = parseInt(strTime.substr(3,2), 10); var ss = parseInt(strTime.substr(6,2), 10); ss ++; if(ss == 60) { ss = 0; mm ++; if( mm == 60 ) { mm = 0; hh ++; if( hh == 24 ) { hh = 0; } } }
document.getElementById("txt1").value = (( hh > 9 ? hh : "0" + hh ) + ":" + ( mm > 9 ? mm : "0" + mm ) + ":" + ( ss > 9 ? ss : "0" + ss )); } //--> </SCRIPT> </HEAD> <BODY> <input type="text" id="txt1" readonly="ture" value="02:58:54" /> </BODY> </HTML>
我晕 完全错光了 你当JAVA写呢,JS是弱类型语言,应该是这样<script language="javascript"> displaytime();function displaytime() { var strTime = document.getElementById("<%=txtCurTime.ClientID %>").innerText; //parseInt是Js内置的函数,int不是 hh = parseInt(strTime.substr(0,2)); mm = parseInt(strTime.substr(3,2)); ss = parseInt(strTime.substr(6,2)); if(ss>=59) { ss = 0; mm ++; } else { ss ++; } if(mm>=59) { mm = 0; hh ++; } else { mm ++; } document.getElementByID("<%=txtCurTime.ClientID %>").innerText = FormatTime(hh, mm, ss); //setTimeout使用时不能用参数,所以用new Function传递一下 setTimeout(new Function("displaytime()"), 1000); }function FormatTime(hh, mm, ss) { var strRet = ""; if(hh > 9) { strRet += "0"; } //最好用String直接格式化,当然你自己定义了toString()也可以 strRet += String(hh) + ":"; if(mm > 9) { strRet += "0"; } strRet += String(mm) + ":"; if(ss > 9) { strRet += "0"; } strRet += String(ss); return strRet; } </javascript>
你函数的第一行无非是想声明此变量为字符型的而已啊。js是弱类型的,直接对变量赋一个字符串空值就可以了 var strTime=""; 刚才测试了一下,已经可以了。还有下面的几句不知道lz是想取长度还是想强制类型转换,js不需要这样的,这样也出错。 hh = strTime.substr(0,2); mm = strTime.substr(3,2); ss = strTime.substr(6,2);
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Philoo">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
setInterval('displaytime()', 1000); function displaytime()
{
var strTime = document.getElementById("txt1").value;//
var hh = parseInt(strTime.substr(0,2), 10);
var mm = parseInt(strTime.substr(3,2), 10);
var ss = parseInt(strTime.substr(6,2), 10);
ss ++;
if(ss == 60)
{
ss = 0;
mm ++;
if( mm == 60 )
{
mm = 0;
hh ++;
if( hh == 24 )
{
hh = 0;
}
}
}
document.getElementById("txt1").value = (( hh > 9 ? hh : "0" + hh ) + ":" + ( mm > 9 ? mm : "0" + mm ) + ":" + ( ss > 9 ? ss : "0" + ss ));
}
//-->
</SCRIPT>
</HEAD> <BODY>
<input type="text" id="txt1" readonly="ture" value="02:58:54" />
</BODY>
</HTML>
完全错光了
你当JAVA写呢,JS是弱类型语言,应该是这样<script language="javascript">
displaytime();function displaytime()
{
var strTime = document.getElementById("<%=txtCurTime.ClientID %>").innerText;
//parseInt是Js内置的函数,int不是
hh = parseInt(strTime.substr(0,2));
mm = parseInt(strTime.substr(3,2));
ss = parseInt(strTime.substr(6,2));
if(ss>=59)
{
ss = 0;
mm ++;
}
else
{
ss ++;
}
if(mm>=59)
{
mm = 0;
hh ++;
}
else
{
mm ++;
}
document.getElementByID("<%=txtCurTime.ClientID %>").innerText = FormatTime(hh, mm, ss);
//setTimeout使用时不能用参数,所以用new Function传递一下
setTimeout(new Function("displaytime()"), 1000);
}function FormatTime(hh, mm, ss)
{
var strRet = "";
if(hh > 9)
{
strRet += "0";
}
//最好用String直接格式化,当然你自己定义了toString()也可以
strRet += String(hh) + ":";
if(mm > 9)
{
strRet += "0";
}
strRet += String(mm) + ":";
if(ss > 9)
{
strRet += "0";
}
strRet += String(ss);
return strRet;
}
</javascript>
var strTime="";
刚才测试了一下,已经可以了。还有下面的几句不知道lz是想取长度还是想强制类型转换,js不需要这样的,这样也出错。
hh = strTime.substr(0,2);
mm = strTime.substr(3,2);
ss = strTime.substr(6,2);