用于List值

时间:2016-03-29 11:47:17

标签: javascript css loops while-loop

在下面的内容中,我想迭代"月"中可用的值。 while循环中的变量。     在我尝试时,它会显示信息为"期望整数值"。

if (selectedForYear() == 2016) {
   var d = new Date();
   var month = d.getMonth();
   while (month <= 11) {
     $('.accordionContent .flexContent .dk_options li:nth-child(month)').css({
       "display": 'none'
     });
     n++;
   }
}

有没有办法在while循环中为css&#34; li:nth-​​child(月)&#34;迭代月值。

提前致谢。

2 个答案:

答案 0 :(得分:1)

两个错误:

month不得位于字符串文字…nth-child(month)内。此外,您可能希望增加month,而不是n

while (month <= 11) {
  $('.accordionContent .flexContent .dk_options li:nth-child(' + month + ')').css({ "display": 'none' });
  month ++;
}

答案 1 :(得分:0)

尝试使用parseInt(),缺少的是与变量的串联。您需要将变量的值与选择器

连接起来
$('.accordionContent .flexContent .dk_options li:nth-child('+ parseInt(month)+')').css({ "display": 'none' });