简体   繁体   English

如何使用document.getElementById在倒数计时器中为输出元素添加类(样式)?

[英]How can I add class(style) for output elements in countdown timer using document.getElementById?

I've done my countdown timer simple but now I would like to add style for it in Javascript.How can I do this?Is it possible to output value different?If yes,how? 我已经简化了倒数计时器,但是现在我想在Javascript中为其添加样式。我该怎么做?是否可以输出不同的值?如果可以,如何?

Here is source 这是来源

 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> 

Your Fiddle link is empty, but from the title of the question I guess, you want to add a CSS class to an element that you get using getElementById() in pure javascript. 您的Fiddle链接为空,但是我想从问题的标题开始,向要使用纯JavaScript的getElementById()获取的元素添加CSS类。 If so, here is an option: 如果是这样,这是一个选择:

document.getElementById('your_element_id').classList.add('class_to_add');

Here is a page that explains how to set an element's style by changing its class or by manipulating its style. 这是一个页面,说明如何通过更改元素的类或操纵其样式来设置元素的样式。

https://www.w3schools.com/jsref/met_element_setattribute.asp https://www.w3schools.com/jsref/met_element_setattribute.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM