我已经简单地完成了我的倒数计时器,但现在我想在 Javascript 中为其添加样式。我该怎么做?是否可以输出不同的值?如果是,如何?
这是源码
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
.countdown
{
margin:0 auto;
font-weight: 100;
font-size:3em;color: white;
width: 100%;
text-align: center;
background-image:blue;
}
<div class="countdown" id="demo"></div>
最佳答案
您的 Fiddle 链接是空的,但从问题的标题我猜,您想将 CSS 类添加到您在纯 javascript 中使用 getElementById()
获得的元素。如果是这样,这里有一个选项:
document.getElementById('your_element_id').classList.add('class_to_add');
关于javascript - 如何使用 document.getElementById 为倒数计时器中的输出元素添加类(样式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42582682/