1.遍歷數組 var arr = [ "one", "two", "three", "four"]; $.each(arr, function(index, value){ alert(this); //this指向當前元素 //index表示Array當前下標//value表示Array當前元素 }); //上面這個each輸出的結果分別爲:one,two,three,four var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]] $.each(arr1, function(index, item_list){ alert(item_list[0]); }); //因此上面這個each輸出分別爲:1 4 7 2遍歷字典 var obj = { one:1, two:2, three:3, four:4}; $.each(obj, function(key, val) { alert(obj[key]); }); //輸出結果爲:1 2 3 4 3.$.each遍歷json對象 var json = [ {"id":"1","tagName":"apple"}, {"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"}, {"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"} ]; $.each(json, function(index, obj) { alert(obj.tagName); }); 在Chrome中,它顯示在控制檯下面的錯誤: Uncaught TypeError: Cannot use 'in' operator to search for '156' in [{"id":"1","tagName":"apple"}... 解決方案:JSON字符串轉換爲JavaScript對象。 var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"}, {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"}, {"id":"5","tagName":"pineapple"}]'; $.each(JSON.parse(json), function(idx, obj) { alert(obj.tagName); }); //or $.each($.parseJSON(json), function(idx, obj) { alert(obj.tagName); });