for() 和$.each()的用法區別


一、對於數組

var arr=['姚明','易建聯','張繼科'];

 




$.each(arr,function(index,value){
document.write(index+"="+value+"</br>");

});


for(var i in arr){
document.write(i+"</br>");   //獲得數組下標
document.write(arr[i]+"</br>");
}

 

二、對於對象

var object={name:"姚明",sex:'man'};
var o2={name="姚明",sex='man'};這么定義對象是錯誤的

$.each(o,function(key,value){
document.write(key+"="+value+"</br>");

});

 

for(var i in o){

document.write(i+"</br>");   // 獲得鍵值對的鍵key
document.write(o[i]+"</br>");
}

 

注:for()遍歷對象或者數組其格式一樣  document.write(arr[i]+"</br>");  document.write(o[i]+"</br>");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM