[b]jquery的$().each,$.each的差別與詳解[/b]
[b]$().each[/b]
對比對上的每個DOM對象調用一個處理(函數)
$().each,對于這個方法,在dom處理上面用的較多
如:
$(“input[name=’ch’]”).each(function(i){
if($(this).attr(‘checked’)==true)
{
//一些操作代碼
}
[b]$.each()[/b]
用于周遊一個對象
[b]周遊一個數組[/b]
$.each([{“name”:”limeng”,”email”:”xfjylimeng”},{“name”:”hehe”,”email”:”xfjylimeng”},function(i,n)
{
alert(“索引:”+i,”對應值為:”+n.name);
});
參數i為周遊索引值,n為目前的周遊對象(數組裡的一個子項).
也可以用i作為下标進行通路
var arr1 = [ “one”, “two”, “three”, “four”, “five” ];
$.each(arr1, function(){
alert(this);
});
周遊每一個數組項:
輸出:one two three four five
var arr1 = ["one", "two", "three", "four", "five"];
$.each(arr1, function (i,n) {
alert(i);
alert(arr1[i]);
});
周遊每一個數組項:
輸出:one two three four five
i為周遊索引值
n為數組項
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){
alert(item[0]);
});
i為周遊索引值
item為二維數組裡的一個子項(一維數組)
輸出:1 4 7
var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});
key為對象裡(key/value)裡的key
val為對象裡(key/value)裡的value
參考原文:[url]http://www.frontopen.com/1394.html[/url]