javascript时间为什么不显示

来源:百度知道 编辑:UC知道 时间:2024/06/15 23:41:14
<!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>
<script language="javascript">
function disptime()
{
var now= new Date();
var hour=now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
if(hour>=0&&hour<12)
document.write("上午好!");
if(hour>=12&&hour<18)
document.write("下午好!");
if(hour>=18&&hour<24)
document.write("晚上好!");

两个问题:
1、根本就没调用这个方法,需要在onload里将该方法进行调用
<body onload="disptime();">

2、document.write("<p>今天日期:"+now.getYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日");
这句里的now.getMonth()+1外边的括号是中文的(全角),需要改为英文括号(半角)
document.write("<p>今天日期:"+now.getYear()+"年"+(now.getMonth()+1)+"月"+now.getDate()+"日");

(now.getMonth()+1)两边的括号是全角的,改为
(now.getMonth()+1);
然后在</script>上面加上一行
disptime();

你没调用你的函数:disptime();放在</script>前面试试

直接运行一下代码就可以了,有注释

<!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.o