今天下午一直在修改一个函数,,还是理解的不够透彻,错了但是就是不知道为什么,是什么原因
其实是一个很简单的代码,看代码
<!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" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
function checkTime()
{
var hours=document.getElementById("hour").value;
if(hours<6)
{
alert("当前时间是:"+hours+"点"+",还可以再睡会");
}else
{
alert("该起床去上班了");
}
}
</script>
<form method="post" >
<input type="text" name="shijian" id="hour"/>
<input type="button" value="提交时间" onclick="checkTime()" />
</form>
</body>
</html>
在文本框里面输入数字就可以得到相应的结果
我的错误:
我的代码是这样:
<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script type="text/javascript">
function checktime()
{
var hours=document.getElementById("hour").value;
if(hours<6)
{
alert("当前时间是:"+hours+"点"+",还可以再睡会");
checktime();
}else
{
alert("该起床去上班了");
checktime();
}
}
</script>
<form method="post" onsubmit="checktime()">
<input type="text" name="shijian" id="hour"/>
<input type="submit" value="提交时间" />
</form>
</body>
</html>
可以发现,在定义函数checkTime()之后我又在里面调用了它,肯定是不对的,脑子进水了的。
其次 我还有错误就是,在括号里面的字符串之间需要"+"号。我把这个也省略了,原来不小心是会害死人的。。
最后需要注意的是 当你的按钮用的是submit后面或者上面就要用onsubmit
按钮用的是button时,后面就用onclick
多敲代码 多学习吧