JavaScript 时钟无法正确显示
我一直在自学 JavaScript,而且我还是个新手,我尝试制作一个时钟功能来添加到我的网站,但是,我看不到让它显示,这是我的完整代码
<html>
<head>
<script type="text/javascript">
function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
if (intHours == 0) {
hours = "12:";
ap = "Midnight";
} else if (intHours < 12) {
hours = intHours + ":";
ap = "a.m";
} else if (intHours == 12) {
hours = "12:";
ap = "noon";
} else {
intHours = intHours - 12
hours = intHours + ":";
ap = "p.m.";
}
if (intMinutes < 10) {
minutes = "0" + intMinutes + ":";
} else {
minutes = intMinutes + ":";
}
if (intSeconds < 10) {
seconds = "0" + intSeconds + " ";
} else {
seconds = intSeconds + " ";
}
timeString = hours + minutes + seconds + ap;
Clock.innerHTML = timeString;
window.setTimeout("tick();", 100);
}
//--></script>
</head>
<body>
<div id="Clock" align="center" style="font-family: Verdana; font-size: 10px; color:#000000"></div>
</body>
</html>
:我说,我运行它,据我所知,它应该运行良好,正如我所说,我有点新,所以也许有人可以帮助我。
再次感谢人们。
编辑:在任何人说之前,我完全意识到这种事情有预制的工作示例,例如 jQuery 时钟等,但我想从头开始自己制作一个。
I've been teaching myself JavaScript and i'm still fairly new, i've tried to make a clock feature to add to my site, however, I can't see to get it to display, this is my full code:
<html>
<head>
<script type="text/javascript">
function tick() {
var hours, minutes, seconds, ap;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
if (intHours == 0) {
hours = "12:";
ap = "Midnight";
} else if (intHours < 12) {
hours = intHours + ":";
ap = "a.m";
} else if (intHours == 12) {
hours = "12:";
ap = "noon";
} else {
intHours = intHours - 12
hours = intHours + ":";
ap = "p.m.";
}
if (intMinutes < 10) {
minutes = "0" + intMinutes + ":";
} else {
minutes = intMinutes + ":";
}
if (intSeconds < 10) {
seconds = "0" + intSeconds + " ";
} else {
seconds = intSeconds + " ";
}
timeString = hours + minutes + seconds + ap;
Clock.innerHTML = timeString;
window.setTimeout("tick();", 100);
}
//--></script>
</head>
<body>
<div id="Clock" align="center" style="font-family: Verdana; font-size: 10px; color:#000000"></div>
</body>
</html>
As I said, I run it and as far as I can see, it should run fine, as I said i'm a bit new, so maybe someone could help me out.
Thanks again people.
EDIT: Before anyone says, I am fully aware that there are premade working examples of this kind of thing, such as jQuery clocks etc, but I wanted to make one myself from scratch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它没有开始,主要是因为你最初需要至少调用一次该函数:
无论你从哪里学习 HTML 和 JavaScript,立即停止从那里学习;你正在学习的代码习惯和方法是非常非常差和过时的。
问题包括:
div#Clock
作为全局变量Clock
访问,该变量已弃用,以及应该是document.getElementById('Clock');
setTimeout
,而实际上应该是setTimeout(tick, 100);
align
属性,而您应该在text-align: center;
通过style
属性使用内联 CSS 的It is not starting, mainly because you need to call the function at least once initially:
Where ever you are learning HTML and JavaScript from, stop learning from there immediately; the code habits and methods you are learning are very, very poor and outdated.
Problems include:
<!DOCTYPE html>
div#Clock
as a global variableClock
, which is deprecated, and should bedocument.getElementById('Clock');
setTimeout
, when it should really besetTimeout(tick, 100);
align
attribute, when you should usetext-align: center;
in CSSstyle
attribute, which constitutes poor separation of presentation and content首先,您需要在某处运行函数 tick() 。如果你只定义它,它不会做任何事情。以正文
onLoad
事件为例。其次,您的元素应该在 var 中正确设置。您应该通过 id 获取元素:
First, you need to run your function tick() somewhere. It doesn't do anything if you only define it. In the body
onLoad
event for example.Second, your element should be properly set in a var. You should get the element by id:
只需将最后两行更改为
<罢工>
> <罢工>
演示
:http://jsfiddle.net/naveen/3PQ8B/1/
更新:错过
document.getElementById
部分:)Just change your last two lines from
to
Demo: http://jsfiddle.net/naveen/3PQ8B/1/
Update: missed the
document.getElementById
part :)