简体   繁体   English

将CSS类或跨度添加到.js变量

[英]Add a css class or span to a .js variable

I have a working countdown timer "days, hours, minutes, seconds" and I need to change the color of the seconds "var" from white to yellow while the other var's will keep the white color. 我有一个工作的倒数计时器,“天,小时,分钟,秒”,我需要将秒“ var”的颜色从白色更改为黄色,而其他var则保持白色。

The problem is that the whole countdown date is being placed on a single div and I cannot add a specific class or a span to the "seconds" variable. 问题是整个倒计时日期都放在一个div上,而我无法在“ seconds”变量中添加特定的类或跨度。

I tried many different solutions but none seems to work in this case so I decided to ask for help and make a Question. 我尝试了许多不同的解决方案,但在这种情况下似乎都无法解决,因此我决定寻求帮助并提出问题。

The HTML. HTML。

<div class="container">
  <div id="countdown" align="center"> <!-- The countdown is being displayed here -->

  </div>
</div>

The JS. JS。

CountDownTimer('06/01/2016 06:00 AM', 'countdown');

    function CountDownTimer(dt, id)
    {
        var end = new Date(dt);

        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(id).innerHTML = 'end';

                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 + ' ';
            document.getElementById('countdown').innerHTML += hours + ' ';
            document.getElementById('countdown').innerHTML += minutes + ' ';
            document.getElementById('countdown').innerHTML += seconds + ' ';


        }

        timer = setInterval(showRemaining, 1000);
    }   

Working JS FIDDLE here where you can see for yourself: 在这里工作JS FIDDLE,您可以自己看到:

https://jsfiddle.net/baqc6nx2/1/ https://jsfiddle.net/baqc6nx2/1/

And here is the image explaining how the seconds var should be colored yellow and the others white. 这是解释秒变var应该被染成黄色而其他变白的图像。

它应该看起来如何。第二个变量显示为黄色。

Sorry for my bad English, and thank you for your time. 对不起,我的英语不好,谢谢您的宝贵时间。

EDIT:--------------------------------------------------------------- 编辑: - - - - - - - - - - - - - - - - - - - - - - - - ---------------

This is not a duplicate, I know the last child selectors, but in this case it wont work, I tried. 这不是重复项,我知道最后一个子选择器,但是在这种情况下,它无法正常工作,我尝试过。

Edit----------------------------------------------------------------- 编辑 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------

TGO Helped me and now it is working, please reopen the question so I can complete it. TGO帮助了我,现在它可以正常工作,请重新打开问题,以便我完成。

Here is an updated fiddle which solves your problem. 这是一个更新的小提琴,可以解决您的问题。 http://jsfiddle.net/baqc6nx2/2 http://jsfiddle.net/baqc6nx2/2

For convenience, this is the changed line of code: 为了方便起见,这是更改后的代码行:

document.getElementById('countdown').innerHTML += '<span style="color:yellow;">' + seconds + '</span> ';

Here I create a span tag, give it the proper CSS style to turn the color yellow, and the seconds value is put inside the span element. 在这里,我创建了一个span标记,为其提供了适当的CSS样式以将颜色变为黄色,并将seconds值放入span元素内。

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

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