简体   繁体   English

如何通过日期条件使用javaScript更改CSS样式?

[英]How can I change css styles with javaScript by date condition?

I was wonder if anyone can tell me why this isn't working? 我想知道是否有人可以告诉我为什么这不起作用? I am trying to switch display of div. 我正在尝试切换div的显示。

function testDatum(){
    var vandaag = new Date();
    var eindDatum = new Date('04/15/2018');
    if (vandaag > eindDatum){
        document.getElementById('Div1').style.display = 'none';
    } 
    else {
        document.getElementById('Div2').style.display = 'none';
    }   
}

The code works.... 该代码有效。

 function testDatum(){ var vandaag = new Date(); var eindDatum = new Date('05/15/2018'); if (vandaag > eindDatum){ document.getElementById('Div1').style.display = 'none'; } else { document.getElementById('Div2').style.display = 'none'; } } testDatum() 
 <div id="Div1">Div1</div> <div id="Div2">Div2</div> 

What you have written, It's absolute current but you didn't declare the function. 您编写的内容是绝对最新的,但您没有声明该功能。 So, You didn't get any result. 因此,您没有得到任何结果。

declare function now you get result. 现在声明函数,您将得到结果。 testDatum(); testDatum();

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

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