簡體   English   中英

如何使用document.getElementById在倒數計時器中為輸出元素添加類(樣式)?

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

我已經簡化了倒數計時器,但是現在我想在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鏈接為空,但是我想從問題的標題開始,向要使用純JavaScript的getElementById()獲取的元素添加CSS類。 如果是這樣,這是一個選擇:

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

這是一個頁面,說明如何通過更改元素的類或操縱其樣式來設置元素的樣式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM