最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery中$.each()函數(shù)的用法引申實例

 更新時間:2016年05月12日 16:16:11   作者:天梯夢  
jQuery中的$.each()函數(shù)比forEach更加強大,可以用來遍歷JavaScript中的任意集合,借來下來我們會舉幾個jQuery中$.each()函數(shù)的用法引申實例,首先先來回顧一下遍歷用法的基礎:

語法:

$.each( collection, callback(indexInArray, valueOfElement) ) 

值得一提的是,forEach 可以很方便的遍歷數(shù)組和 NodeList ,jQuery 中的 jQuery 對象本身已經(jīng)部署了這類遍歷方法,而在原生 JavaScript 中則可以使用 forEach 方法,但是 IE 并不支持,因此我們可以手動把 forEach 方法部署到數(shù)組和 NodeList 中:

if ( !Array.prototype.forEach ){
  Array.prototype.forEach = function(fn, scope) {
    for( var i = 0, len = this.length; i < len; ++i) {
      fn.call(scope, this[i], i, this);
    }
  }
}
 
// 部署完畢后 IE 也可以使用 forEach 了
document.getElementsByTagName('p').forEach(function(e){
 
  e.className = 'inner';
});

而jQuery中的$.each()函數(shù)則更加強大。$.each()函數(shù)和$(selector).each()不一樣。$.each()函數(shù)可以用來遍歷任何一個集合,不管是一個JavaScript對象或者是一個數(shù)組,如果是一個數(shù)組的話,回調(diào)函數(shù)每次傳遞一個數(shù)組的下標和這個下標所對應的數(shù)組的值(這個值也可以在函數(shù)體中通過this關鍵字獲取,但是JavaScript通常會把this這個值當作一個對象即使他只是一個簡單的字符串或者是一個數(shù)字),這個函數(shù)返回所遍歷的對象,也就是這個函數(shù)的第一個參數(shù),注意這里還是原來的那個數(shù)組,這是和map的區(qū)別。
其中collection代表目標數(shù)組,callback代表回調(diào)函數(shù)(自己定義),回調(diào)函數(shù)的參數(shù)第一個是數(shù)組的下標,第二個是數(shù)組的元素。當然我們也可以給回調(diào)函數(shù)只設定一個參數(shù),這個參數(shù)一定是下標,而沒有參數(shù)也是可以的。

例1:傳入數(shù)組

<!DOCTYPE html> 
<html> 
<head> 
<script src=”http://code.jquery.com/jquery-latest.js”></script> 
</head> 
<body> 
<script> 

$.each([52, 97], function(index, value) { 
alert(index + ‘: ‘ + value); 
}); 

</script> 
</body> 
</html> 

輸出:

0: 52 
1: 97 

例2:如果一個映射作為集合使用,回調(diào)函數(shù)每次傳入一個鍵-值對

<!DOCTYPE html> 
<html> 
<head> 
<script src=”http://code.jquery.com/jquery-latest.js”></script> 
</head> 
<body> 
<script> 

var map = { 
‘flammable': ‘inflammable', 
‘duh': ‘no duh' 
}; 
$.each(map, function(key, value) { 
alert(key + ‘: ‘ + value); 
}); 

</script> 
</body> 
</html> 

輸出:

flammable: inflammable 
duh: no duh 

例3:回調(diào)函數(shù)中 return false時可以退出$.each(), 如果返回一個非false 即會像在for循環(huán)中使用continue 一樣, 會立即進入下一個遍歷

<!DOCTYPE html> 

<html> 

<head> 

 <style> 

 div { color:blue; } 

 div#five { color:red; } 

 </style> 

 <script src=”http://code.jquery.com/jquery-latest.js”></script> 

</head> 

<body> 

 <div id=”one”></div> 

 <div id=”two”></div> 

 <div id=”three”></div> 

 <div id=”four”></div> 

 <div id=”five”></div> 

<script> 

  var arr = [ "one", "two", "three", "four", "five" ];//數(shù)組 

  var obj = { one:1, two:2, three:3, four:4, five:5 }; // 對象 

  jQuery.each(arr, function() { // this 指定值 

   $(“#” + this).text(“Mine is ” + this + “.”); // this指向為數(shù)組的值, 如one, two 

    return (this != “three”); // 如果this = three 則退出遍歷 

  }); 

  jQuery.each(obj, function(i, val) { // i 指向鍵, val指定值 

   $(“#” + i).append(document.createTextNode(” – ” + val)); 

  }); 

</script> 

</body> 

</html> 

輸出 :

Mine is one. – 1 
Mine is two. – 2 
Mine is three. – 3 
- 4 
- 5 

相關文章

最新評論

汝阳县| 扬中市| 无锡市| 赣州市| 潜山县| 温州市| 咸阳市| 临江市| 兴义市| 奉新县| 青铜峡市| 贵南县| 荔浦县| 徐汇区| 噶尔县| 阜平县| 进贤县| 伊宁县| 福建省| 台东市| 盘锦市| 汕尾市| 巴彦县| 当阳市| 湖南省| 琼海市| 信宜市| 曲靖市| 墨竹工卡县| 关岭| 大石桥市| 巴林左旗| 玉环县| 金寨县| 舒城县| 香河县| 民和| 静宁县| 桐梓县| 陆河县| 延寿县|