<html>
<head>
</head>
<body>
<script type="text/javascript"> var now=new Date(); var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
<div style="background-color:blue;width:400px;height:50px">
document.write(year+"年"+month+"月"+date+"日");
</div>
</script>
</body>
</html>
为什么用浏览器打开这个html文件什么也不显示?菜鸟一枚,求教
<head>
</head>
<body>
<script type="text/javascript"> var now=new Date(); var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
<div style="background-color:blue;width:400px;height:50px">
document.write(year+"年"+month+"月"+date+"日");
</div>
</script>
</body>
</html>
为什么用浏览器打开这个html文件什么也不显示?菜鸟一枚,求教
document.write(year+"年"+month+"月"+date+"日");
</div>
把这个div去掉,就只要 document.write(year+"年"+month+"月"+date+"日");这句,div是html的语句,不要写在script中间。
script之间是不支持这样直接写html代码的。
和</div>你怎么能不做任何处理直接写在script中??<html>
<head>
</head>
<body>
<script type="text/javascript">var now=new Date();var year=now.getFullYear();
var month=now.getMonth()+1;
var date=now.getDate();
</script>
<div style="background-color:blue;width:400px;height:50px">
<script>
document.write(year+"年"+month+"月"+date+"日");
</script>
</div>
</body>
</html>
<head>
</head>
<body>
<script type="text/javascript">
Date.prototype.format=function(a){
var o = {
"M+": this.getMonth() + 1, // month
"d+": this.getDate(), // day
"h+": this.getHours(), // hour
"m+": this.getMinutes(), // minute
"s+": this.getSeconds(), // second
"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
"S": this.getMilliseconds()
};
if (/(y+)/.test(a)) {
a = a.replace(RegExp.$1, (this.getFullYear() + "")
.substr(4 - RegExp.$1.length));
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(a)) {
a = a.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]:("00" + o[k]).substr(("" + o[k]).length));
}
};
return a;
}
function showDate(){
var now=new Date();
document.getElementById('date').innerHTML=now.format("yyyy年MM月dd日 hh:mm:ss");
}
window.onload=function(){
setInterval('showDate()',1000);
}
</script>
<div style="width:400px;height:50px" id="date">
</div>
</body>
</html>要注意的事,实际应用中,如果你的页面中已经存在window.onload事件,那么,你应该将setInterval('showDate()',1000);加入到已经有的事件中去。否则会有一个无法执行