gpt4 book ai didi

javascript - 比更改类名的开关更好的解决方案

转载 作者:行者123 更新时间:2023-11-30 07:26:03 25 4
gpt4 key购买 nike

我创建了一个时钟,连接到 date().getHours/minutes/Seconds显示的图像嵌入到不同的类中。

现在,当我想更改图像时,我每隔一分钟和几小时写一个开关..那可能比谷歌引擎更多的代码。所以我想知道是否有更简单的解决方案。

这是小时开关的一些代码所以当时钟到达第 15 分钟时,它会将 className 更改为一和五。

switch(h){

case 15:
x = hours.appendChild(hour1).className = "clock-digit-one";
x = hours.appendChild(hour2).className = "clock-digit-five";
break
case 16:
x = hours.appendChild(hour1).className = "clock-digit-one";
x = hours.appendChild(hour2).className = "clock-digit-six";
break

default:
x = hours.appendChild(hour1).className = "clock-digit-zero";
x = hours.appendChild(hour2).className = "clock-digit-zero";

}

我创建了一个显示更多代码的 jsFiddle。任何提示都会很棒。 http://jsfiddle.net/Xk49c/2/

谢谢

最佳答案

创建一个人类可读数字数组:

var digits = ["zero", "one", "two", ..., "nine"];

h分解为第一位和第二位:

var hours = Math.floor(h / 10);
var minutes = h % 10;

索引到 digits 以确定您应该使用的类名:

hours.appendChild(hour1).className = "clock-digit-" + digits[hours];
hours.appendChild(hour2).className = "clock-digit-" + digits[minutes];

关于javascript - 比更改类名的开关更好的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com