Heim > Fragen und Antworten > Hauptteil
我写的js代码如下:
setInterval('showTime()',1000);
function showTime() {
var today = new Date();
var hou = today.getHours();
var min = today.getMinutes();
var sec = today.getSeconds();
document.write(hou + ':' + min + ' ' + sec);
}
如果是用alert,则可以实现
alert(hou + ':' + min + ' ' + sec);
但我希望能直接在页面显示,不弹出,貌似做不到,不知道错在哪里,请大家指出,谢谢!
还有alert结果中如何显示空格?用' '
," "
都不行。。。
PHPz2017-04-10 12:50:46
alert 里要显示空格,直接输入空格就行,不需要转义:alert(hou + ':' + min + ' ' + sec);
试了一下,题主的 doument.write 是可以工作的啊。但是建议 setInterval
的第一个参数不要用字符串,用函数。比如:
function showTime() {
/* ... */
}
setInterval(showTime, 1000);