Отображение div в определенное время сбрасывается при изменении месяца - PullRequest
0 голосов
/ 04 февраля 2020

У меня есть div / tab для отображения после 1 февраля этого года. Это прекрасно работает, пока не закончится месяц, а потом div снова скрыт. Я хочу, чтобы div всегда оставался открытым. Первая вкладка должна отображаться 1 февраля, вторая - 14 февраля. Вот мой код.

<style>

.disable-1, .disable-2 {
  display: none;
}

.enabled {
 display: block;
}

</style>

<ul class="tabs">
    <!-- THIS UL CREATES THE TABS == Just add/remove LIs as neeed to create them. Be sure the IDs of the sections match -->
    <li><a href="#PLY-EC-001-1">Tab 1</a></li>
    <li class="disable-1"><a href="">Tab 2</a></li>
    <li class="disable-2"><a href="">Tab 3</a></li>
    <
</ul>

<script>
    // get current date on page load
    var date = new Date();

    // ask for specific date
    if (date.getFullYear() >= 2020 && date.getMonth() >= 1 && date.getDate() >= 1) {
        // get first dom-element with that class 
        var li = document.getElementsByClassName('disable-1'); 

        // if dom-element exists
        if (li.length > 0) {

            // set classname
            li[0].className = 'enabled';
        }           
    }

    if (date.getFullYear() >= 2020 && date.getMonth() >= 1 && date.getDate() >= 14) {
        // get first dom-element with that class 
        var li = document.getElementsByClassName('disable-2'); 

        // if dom-element exists
        if (li.length > 0) {

            // set classname
            li[0].className = 'enabled';
        }           
    }

</script>

Ответы [ 2 ]

1 голос
/ 04 февраля 2020

Вместо проверки года, месяца и текущей даты с помощью логического И , просто проверьте, превышает ли текущая дата указанную c метку времени (2020-02-01: 00: 00:00 в вашем случае):

var date = new Date();
var refd = new Date(2020,1,1,0,0,0); // <-- Reference date

// compare if now (date) is bigger than reference (refd)
// by comparing the milliseconds since 1970-01-01
if (Number(date) >= Number(refd)) // <-- compare
{
    // ... elided, as before
}
0 голосов
/ 04 февраля 2020
<script type="text/javascript">

function displayDiv()
{
   var d = new Date();
   var strDate =  d.getDate();

   if(strDate == '1')
     document.getElementById('HelloWorld').visible= true;
}
</script>


<form onLoad="displayDiv()" method="GET">
<div id="HelloWorld" style="display:none;"></div>
</form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...