如何使用几个月创建数组,然后使用switch语句显示当前月份

时间:2016-06-10 02:39:29

标签: javascript html arrays function switch-statement

我想创建一个存储一年中几个月的数组。我希望能够在像

这样的div中调用它。
 var months = new Array["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];

然后创建一个switch语句,它将获取当前月份并显示与此类似的月份。

switch(num)
  {
    case 0:
      month="January";
      break;
    case 1:
      month="February";
      break;
    case 2:
      month="March";
      break;
    case 3:
      month="April";
      break;
    case 4:
      month="May";
      break;
    case 5:
      month="June";
      break;
    case 6:
      month="July";
      break;
    case 7:
      month="August";
      break;
    case 8:
      month="September";
      break;
    case 9:
      month="October";
      break;
    case 10:
      month="November";
      break;
    case 11:
      month="December";
      break;

我如何在HTML页面中调用它?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

您只需访问该阵列即可。

function getMonth(num){
    return months[num] || "month not found";
}

getMonth(0); //January

getMonth(1);//February

getMonth(12);//month not found, since the array counts start from 0.

我不确定你想要什么,但这里是html代码,有各种查找月份名称的方法。 jQuery会使一些操作更加跨浏览器友好,但你没有列出jQuery,所以我用纯javascript做了这个。

<body>
    <select id='allMonths'></select><br/><br/>
    <input id='txtMonth' type='text' value='1'/><br/>
    <p id='monthName'>Click button below to change based on text box value</p>
    <a href='' onclick='event.preventDefault(); changeMonth();'>Click me to change month based on text box value</a><br/><br/>
    <div id='allMonthsListed'></div>
    <script type='text/javascript'>
        var months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"],
            allMonths = document.getElementById('allMonths'),
            txtMonth = document.getElementById('txtMonth'),
            monthName = document.getElementById('monthName'),
            allMonthsListed = document.getElementById('allMonthsListed');
        function getMonth(num){
            return months[num] || "month not found";
        }
        for(var idx = 0; idx < months.length;idx++){
            var option = document.createElement("option");
            option.innerText = months[idx];
            allMonths.appendChild(option);
        }
        function changeMonth(){
            var month = txtMonth.value;
            if(/^\d+$/.test(month)){
                monthName.innerText = getMonth(parseInt(month) + 1);
            }else{
                monthName.innerText = "Must enter number";
            }
        }

        for(var idx = 0; idx < months.length;idx++){
            var p = document.createElement("p");
            p.innerText = months[idx];
            allMonthsListed.appendChild(p);
        }

    </script>
</body>

答案 1 :(得分:0)

您无需创建非常简单的开关状态

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var d = new Date();
console.log(monthNames[d.getMonth()])

答案 2 :(得分:-1)

两个文件:

您的javascript方法。

methods.js

var months =  ["January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"];

function monthsOfYear() {

    var num = document.getElementById('numberMonth').value;

    document.getElementById('nameOfMonth').innerHTML = months[num];
}

和html页面

page.html

<div>
    <input type="text" id="numberMonth">
    <button onclick="monthsOfYear()">Click</button>
</div>

//will show the name of the month chosen
<div id='nameOfMonth'></div>

//import the javascript file
<script src='methods.js'></script>

这是执行此操作的基本方法。 由于数组从0开始计数,因此数字12将是未定义的,因此您需要解决此问题。 您可以使用jquery或angularjs来改善您的工作。 我希望有帮助。