Javascript的菜鸟问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 12:30:42
一段显示时间的代码,想让它自动刷新
代码如下
<script language="javascript">
function gettime()
{
var dt = new Date()
var str1 = "The time is now : " + dt
document.write=str1;
}
setInterval("gettime()",1000);
</script>
不知道错在什么地方...

function gettime()
{
var dt = new Date()
var str1 = "The time is now : " + dt
document.body.innerHTML += str1 + "<BR>";
}
setInterval("gettime()",1000);

dt是你定义的一个对象,需要附加上相应的属性,才能显示时间。

document.write=str1应该不行吧,应该改为document.write(str1);

<script language="javascript">
function showtime()
{
var now = new Date()
var hours = now.getHours()
var minutes = now.getMinutes()
var seconds = now.getSeconds()
var day=now.getDate();
var month=now.getMonth();
var year=now.getYear();
var week=now.getDay();
var timeValue=""+year+"-";
timeValue +=month+1+"-"
timeValue +=day+"- ";
timeValue +=hours;
timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
timeValue += ((seconds < 10) ? ":0" :