Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

javascript - alternative way to show certain icons according to date Using IF statement

Hi I have this working function (have some problems) but I wonder if I can improve on it o change the way of how it works

Explanation: this function takes text Content of a div compare it to a current day at hand then it does the if on it and show certain icons according if the condition is met or not.

Example if today is 29.2 the text content of the div is 29 and I want to show the icons only if the day is old so if the date is like future or tomorrow I want the icons to be hidden … the problem that I am facing btw is this logic works until the month switch to new month and the problem is that the new month numbers are like 1.2.3 small numbers so the icons show up because the if looks at bigger than the current day not smaller ..

any idea how to keep them hidden

protentional complex solution

making a hidden span inside every div which will show the day and the month and then make the logic filter according to month/day rather then only day!! too complex right do you have any more simple idea ? to do this job.

//hide Task controle (archive Trash) if day is older than today
function hideIcons() {
    // Extermal lets !!! already defined Trash and Archive Array style too !! FIX!!!  
    let divs = document.getElementsByClassName('day-number');
    let trash = document.getElementsByClassName('fa-trash-alt');
    let archive = document.getElementsByClassName('fa-archive');
    let today = new Date();
    let s = today.getDate()
    for (let i = 0; i < divs.length; i++) {
        (() => {
            if (divs[i].textContent >= s) {
                trash[i].style.display = 'none';
                archive[i].style.display = 'none';
            };
        })(i);
    }
};
hideIcons();
<span class="day-number"></span>
question from:https://stackoverflow.com/questions/65948148/alternative-way-to-show-certain-icons-according-to-date-using-if-statement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...