我試圖迭代並輸出選項屬性的值使用jquery each()。我的代碼似乎輸出數組索引,而不是字符串值。爲什麼是這樣?這是我jsfiddle迭代jquery對象
var allQuestions = [
{
question: "Who is Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
correctAnswer:0
}];
$(info[0].choices).each(function(value){
$('#answers').append($('<li>').text(value));
});
回調的第一個參數是'index',你應該使用第二個參數。 – undefined 2014-09-10 17:44:41
給每個()回調的參數都搞亂了。你想爲價值的第二個參數,不要問我爲什麼arity是這樣的。 – dandavis 2014-09-10 17:44:45
如果您使用的是不熟悉的方法,我建議[先閱讀**文檔**](http://api.jquery.com/each/)。 *「爲什麼是這樣?」*因爲這就是該方法的工作原理。 – 2014-09-10 17:49:38