简体   繁体   English

如何按月份显示内容

[英]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.我正在尝试制作一个 html 页面,如果选择月份,正确的日期将显示在表格中。 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>

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

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