2016-03-29 50 views
-1

在下面的內容中,我想迭代while循環中「month」變量中可用的值。 雖然我正在嘗試,它顯示信息爲「預計整數值」。儘管在CSS選擇器中循環迭代列表值

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「li:nth-​​child(month)」迭代月份值。

在此先感謝。

+0

這裏'( '.accordionContent .flexContent .dk_options李:第n個孩子(月)')',一個月沒有指向變量'month'但被視爲字符串'month',試試這個'( '.accordionContent .flexContent .dk_options li:nth-​​child('+ month +')') – Rajesh

+0

@Rajesh它不會工作。當我嘗試上述方式時,我無法在下拉菜單中選擇「2016」。 – Kamal

回答

1

兩個錯誤:

month不能是字符串文字…nth-child(month)內。此外,您可能想增加month,而不是n

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

太棒了。有效。謝謝。 – Kamal

0

嘗試使用parseInt(),缺少的是與變量的連接。你需要與選擇來連接變量的值

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

它不會工作。雖然我嘗試像上面那樣,但是我無法在用戶界面的下拉菜單中選擇「2016」特定值。 – Kamal