欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery的$().each和$.each的區(qū)別

 更新時間:2019年01月18日 09:51:05   作者:muzidigbig  
今天小編就為大家分享一篇關(guān)于jquery的$().each和$.each的區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

$(selector).each(function(index,element))

這個函數(shù)和之前項目里面用到的遍歷數(shù)據(jù)的函數(shù)不是同一個呀(項目里面用到的函  數(shù):$.each(dataresource,function(index,element))),于是,就好好研究了下,果然在JS里面有兩個相似的函數(shù),于是也就有了今天的主題:

 1.$(selector).each(function(index,element))

 2.$.each(dataresource,function(index,element))

接下來就對這兩個函數(shù)做深入的探討:

1.$(selector).each(function(index,element))

作用:在dom處理上面用的較多

  <ul id="each_id">
    <li>muzi</li>
    <li>digbig</li>
    <li>muzidigbig</li>
  </ul>

js遍歷函數(shù):

  function traversalDOM(){
    $("#each_id li").each(function(){
       alert($(this).text())
    });
  }
  traversalDOM();

輸出結(jié)果:

2.$.each(dataresource,function(index,element))

作用:在數(shù)據(jù)處理上用的比較多

示例:

此處沒有html代碼,只有js代碼,如下:

  function traversalData(){
    var jsonResourceList = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},{"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"}]';
    if(jsonResourceList.length >0){
      $.each(JSON.parse(jsonResourceList), function(index, currentObj) {
        alert(currentObj.tagName);
      });
    }
  }
  traversalData()

3.最終結(jié)論:

在遍歷DOM時,通常用$(selector).each(function(index,element))函數(shù);

在遍歷數(shù)據(jù)時,通常用$.each(dataresource,function(index,element))函數(shù)。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論