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

jQuery中each方法的使用詳解

 更新時(shí)間:2018年03月18日 10:05:19   作者:Tel大神  
each() 方法規(guī)定為每個(gè)匹配元素規(guī)定運(yùn)行的函數(shù)。這篇文章主要給大家介紹jQuery中each方法的使用詳解,需要的朋友參考下吧

概述:

  each() 方法規(guī)定為每個(gè)匹配元素規(guī)定運(yùn)行的函數(shù)。

  返回 false 可用于及早停止循環(huán),相當(dāng)于break。

  返回 true 可以結(jié)束本次循環(huán),相當(dāng)于continue。

語(yǔ)法:

$(selector).each(function(index,element){ })
    index - 選擇器的 index 位置
    element - 當(dāng)前的元素(也可使用 "this" 選擇器)
  $(selector).each(function(){ })
  $.each(array,function(Key,Value){ })

1.遍歷js數(shù)組

$(function(){
 var array=["aaa","bbb","ccc"];
 $.each(array,function(i,j){
  alert(i+":"+j);  //i表示索引,j代表值
 });
}) 

2.遍歷Object對(duì)象

var obj = new Object();
obj.name="zs";
$.each(obj, function(name, value) {
  alert(this);  //this指向當(dāng)前屬性的值,等價(jià)于value
  alert(name);  //name表示Object當(dāng)前屬性的名稱
  alert(value);  //value表示Object當(dāng)前屬性的值
});

3.遍歷JSON對(duì)象

var json ={"name":"zhangSan","role":"student"};
$.each(json,function(key,value){
 alert(key+":"+value);
});

4.遍歷由多個(gè)JSON對(duì)象組成的數(shù)組

var json =[{"name":"Amy","role":"student"},{"name":"Tom","role":"student"}];
$.each(json, function(index, value) {
  alert("index="+index+"\n" +"name:"+value.name+"\n"+"role:"+value.role+"\n");
});

 5.遍歷jQuery對(duì)象 

<head>
    <meta charset="utf-8" />
  <title>遍歷jQuery對(duì)象</title>
  <script src="js/jquery-1.12.4.js"></script>
  <script type="text/javascript">
   $(function(){
    $("input[type='button']").bind("click",function(){
     $("li").each(function(){
      alert($(this).text())
      });
    });
   });
  </script>
 </head>
 <body>
  <input type="button" value="觸發(fā)事件"/>
  <ul>
   <li>first</li>
   <li>second</li>
  </ul>
 </body>

總結(jié)

以上所述是小編給大家介紹的jQuery中each方法的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論