我在解决这个问题时遇到了一些麻烦。我想循环遍历所有月份一次,而不是像我的代码现在这样做的那样循环遍历十二次。任何人都可以帮忙,还有更有效的方法来循环这个函数吗?
谢谢!
window.onload = function() {
getMonth();
};
function getMonth()
{
var x="",i=0;
var month=Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
while (i<12)
{
x=x + month + "<br/>";
i++;
}
document.getElementById("months").innerHTML=x;
}
这是我的 jsfiddle 演示的链接 http://jsfiddle.net/priswiz/LqEE6/
最佳答案
只需使用 Array .join 方法即可。
var x = month.join("<br />");
如果您使用x=x + month[i] + "<br/>";
,您的代码将正常工作
关于javascript - 循环遍历 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441757/