简体   繁体   中英

How to display content according to the month

I am trying to make a html page where if the month is picked, the correct dates will display in a table. I have a function where it gets todays month and the user is able to switch between the months. But I am unsure on how I can get all of the days. I dont need the numbers to be displayed, just the right about of table rows/table data for the months days.

 var month = new Date(); //nytt datum var index = month.getMonth(); var months = ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"]; document.getElementById("todayField").innerHTML = months[month.getMonth()]; //posta dagens datum by default function next() { var months = ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"]; var nextMonth = index + 1 > 11 ? 0 : index + 1; index = nextMonth document.getElementById("todayField").innerHTML = months[nextMonth]; } function prev() { var months = ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"]; var nextMonth = index - 1 < 0 ? 11 : index - 1; index = nextMonth // console.log(nextMonth) document.getElementById("todayField").innerHTML = months[nextMonth]; } document.getElementById("prev").addEventListener("click", function() { prev(); }) document.getElementById("next").addEventListener("click", function() { next(); })
 <p>Months</p> <button type="button" name="btnPrev" onclick="prev()"><</button> <button type="button" name="btnNext" onclick="next()">></button> <p id="todayField"></p> <p>You can find the days below</p>

May be your problem will solve from below code correction.

 var month = new Date(); //nytt datum var index = month.getMonth(); var months = ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"]; document.getElementById("todayField").innerHTML = months[month.getMonth()]; //posta dagens datum by default function next() { index = (index + 1) > 11 ? 0 : (index + 1); document.getElementById("todayField").innerHTML = months[index]; } function prev() { index = (index - 1) < 0 ? 11 : index - 1; document.getElementById("todayField").innerHTML = months[index]; }
 <p>Months</p> <button type="button" name="btnPrev" onclick="prev()"><</button> <button type="button" name="btnNext" onclick="next()">></button> <p id="todayField"></p> <p>You can find the days below</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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