在SPAN中交换JS元素,使用Class进行输出

时间:2015-02-07 22:55:13

标签: javascript html countdown

我使用下面的代码来计算日期和时间。输出显示为<div id="countdown">203days 20hrs 44mins 31secs</div>。我想将<span class="num">中的数字和<span class="label">中的标签包装起来。我设法做了标签,但我不知道在哪里做数字。有什么帮助吗?

<script>
var end = new Date('08/30/2015 03:00 PM');

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'AWWWWW SHIT!';

            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = days + '<span class="label">days</span>';
        document.getElementById('countdown').innerHTML += hours + '<span class="label">hrs</span>';
        document.getElementById('countdown').innerHTML += minutes + '<span class="label">mins</span>';
        document.getElementById('countdown').innerHTML += seconds + '<span class="label">secs</span>';
    }

    timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>

0 个答案:

没有答案