我想阅读所有课程(7月,8月等)并写入数组。有了这个数组,我想填充一个Dropdown。到目前为止,我有以下内容,但问题是我得到的数组中只有7月出现。希望可以有人帮帮我。谢谢!
var classList =$('span').attr('class').split(/\s+/);
$.each( classList, function(index, item){
console.log(classList);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span class="date Juli firstChild">16.07.2015</span>
</div>
<div>
<span class="date Juli firstChild">21.07.2015</span>
</div>
<div>
<span class="date August firstChild">01.08.2015</span>
</div>
<div>
<span class="date August firstChild">16.08.2015</span>
</div>
<div>
<span class="date August firstChild">31.08.2015</span>
</div>
&#13;
答案 0 :(得分:1)
如果你可以修改HTML,那么将月份名称放在数据属性中然后循环遍历那些生成你的数组会更加语义化,更简单。试试这个:
var months = [];
$('.date').each(function() {
var month = $(this).data('month');
if (months.indexOf(month) == -1)
months.push(month);
})
// create an option in the select per month
for (var i = 0; i < months.length; i++) {
$('select').append('<option>' + months[i] + '</option>');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<span class="date firstChild" data-month="Juli">16.07.2015</span>
</div>
<div>
<span class="date firstChild" data-month="Juli">21.07.2015</span>
</div>
<div>
<span class="date firstChild" data-month="August">01.08.2015</span>
</div>
<div>
<span class="date firstChild" data-month="August">16.08.2015</span>
</div>
<div>
<span class="date firstChild" data-month="August">31.08.2015</span>
</div>
<select name="months"></select>
&#13;