请高手看看下面的脚本代码错在哪儿?
页面是ASP.net,在页面中有一个服务器控件,用来显示服务器上的当前时间,下面的脚本是在客户端显示服务器时间的代码,但总是报告第一行“var strTime: String;”缺少 "; ",如果把第一行去掉,则又报告“hh=int(strTime.substr(0,2));”缺少对象。请高手看看,我的脚本代码错在哪?
<script language= "javascript ">
displaytime();
function displaytime()
{
var strTime: String;
strTime = document.getElementById( " <%=txtCurTime.ClientID %> ").innerText;
hh = int(strTime.substr(0,2));
mm = int(strTime.substr(3,2));
ss = int(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(displaytime(), 1000);
}
function FormatTime(hh:int, mm:int, ss:int): String
{
var strRet: String;
strRet = " ";
if(hh > 9)
{
strRet += "0 ";
}
strRet += hh.tostring() + ": ";
if(mm > 9)
{
strRet += "0 ";
}
strRet += mm.tostring() + ": ";
if(ss > 9)
{
strRet += "0 ";
}
strRet += ss.tostring();
return strRet;
}
</javascript>
[解决办法]
var strTime: String;??直接
var strTime;就可以了吧?
你试一下
[解决办法]
int 是个什么东西
定义了吗?
[解决办法]
看不懂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);
