$().each() 與 $.each()區別,以及 jquery ajax 應用

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

在jquery 中我們可以選擇$().each() 與 $.each() 進行迭代對象和數組 $(items).each(function(){     //item})   ,而後者則  $.each(items,function(i,item){//write your code})  -----------------jquery   ajax----------------------jquery 開發ajax 相對來說 是比較簡單的  具體情況可以參考 [QLeelulu的ajax系列]1.ajax 讀取 XML //XMLFile.xml<?xml version="1.0" encoding="utf-8" ?><users>  <user sex="0">    <name>abcd1</name>    <age>12</age>  </user>  <user sex="1">    <name>abcd2</name>    <age>13</age>  </user></users>   $(function(){               $(".getData").click(function(){                      $.ajax({              url:"XMLFile.xml",              success:function(data)                       {                         $(data).find("user").each(function(){                                                  alert($(this).attr("sex"));//擷取屬性                         alert($(this).children("age").text());//擷取item 中的 age                        })                        },             beforeSend:function(){               $(".loading").html("資料載入中.");                       },              error:function(){                            },              golable:false,                            datatype:"xml",              complete:function()              {                  $(".loading").html("");                    $(".loading").html("資料載入完成");              }           })                   });    }) 2.  jquery 讀取 json   json 格式:                    $.ajax({              url:"index.aspx",              success:function(json)                       {               json=eval_r("("+json+")");//將資料轉成json格式                                        $.each(json,function(i,item){                     document.write("UserID"+String(item.userID)+"   Name:"+item.Name+"   Email:"+item.Email+"<br/>");        });                                                     },              beforeSend:function(){                                    },              error:function(){                            },              golable:false,              datatype:"json",              complete:function()              {                              }           }) 

-------------------------JSON資料擷取---------------------$().each() <wbr>與 <wbr>$.each()區別,以及 <wbr>jquery <wbr>ajax <wbr>應用$().each() <wbr>與 <wbr>$.each()區別,以及 <wbr>jquery <wbr>ajax <wbr>應用Code window.onload=function(){ getJSON();//擷取多組資料 getData();//擷取單組資料 } function getJSON() { var json="[{‘UserID‘:11, ‘Name‘:‘rddy‘, ‘Email‘:‘zhuleiprohotmail.com‘},{‘UserID‘:12, ‘Name‘:‘Tfddly‘, ‘Email‘:‘jjjail.com‘},{‘UserID‘:351, ‘Name‘:‘Truly‘, ‘Email‘:‘zhulm‘}]"; json=eval_r("("+josn+")");//將字串轉換成josn格式 $.each(json,function(i,item){ document.writeln("UserID"+String(item.userID)+" Name:"+item.Name+" Email:"+item.Email+"<br/>"); }); } function getData() { var json="{‘userID‘:12,‘name‘:[‘rhythm‘,‘rhythmk‘]}"; json=eval_r("("+json+")"); document.write(json.name[1]);//rhythmk document.write(json.name);//rhythm,rhythmk

 

$().each() 與 $.each()區別,以及 jquery ajax 應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.