javascript - 如何设置这个 javascript 倒数计时器的样式

标签 javascript html timer

我一直在学习有关如何创建 JavaScript 倒数计时器的教程。我想在 <h1> 中显示每个标记,以便我可以使用 CSS 对其进行样式设置,但到目前为止我的所有尝试都失败了,老实说我不知道​​如何去做。感谢任何帮助

HTML

<span id="countdown"></span> 

Javascript

// set the date we're counting down to
var target_date = new Date("Aug 15, 2019").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s";  

}, 1000);

最佳答案

没有必要使用标签。改为设置 span 的样式。

span#countdown {
    font-size: 48px;
    border: 3px solid black;
    padding: 5px;
    background-color: #ffe;
}

参见 http://jsfiddle.net/8mw6beqh/ .

关于javascript - 如何设置这个 javascript 倒数计时器的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29132880/

相关文章:

javascript - 一次更改 div 内容两次

javascript - dom 元素可见性对 javascript 重要吗

javascript - 六边形 CSS 宽度和高度 %

java - 单击无法正常工作时更改链接文本

python - python threading.Timer 如何确定耗时?

c - 使用 PIC32 的定时器 2 设置延迟

javascript - 如何在 HTML 中切换两个互斥的单选按钮

javascript - 如何添加一个按钮来启用后台脚本?

html - 使输入背景透明并显示闪烁的光标?

asp.net - 如何防止asp :Timer from sending tick before response is generated?