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

JQuery中$.each 和$(selector).each()的區(qū)別詳解

 更新時間:2015年03月13日 10:20:36   投稿:junjie  
這篇文章主要介紹了JQuery中$.each 和$(selector).each()的區(qū)別詳解,本文給出了多個例子講解了它們之間的不同之處,需要的朋友可以參考下

一個通用的遍歷函數 , 可以用來遍歷對象和數組. 數組和含有一個length屬性的偽數組對象 (偽數組對象如function的arguments對象)以數字索引進行遍歷,從0到length-1, 其它的對象通過的屬性進行遍歷.

$.each()與$(selector).each()不同, 后者專用于jquery對象的遍歷, 前者可用于遍歷任何的集合(無論是數組或對象),如果是數組,回調函數每次傳入數組的索引和對應的值(值亦可以通過this 關鍵字獲取,但javascript總會包裝this 值作為一個對象—盡管是一個字符串或是一個數字),方法會返回被遍歷對象的第一參數。

例子:———傳入數組

復制代碼 代碼如下:

<!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

例子:———如果一個映射作為集合使用,回調函數每次傳入一個鍵-值對

復制代碼 代碼如下:

<!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

例子:———回調函數中 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" ];//數組
    var obj = { one:1, two:2, three:3, four:4, five:5 }; // 對象
    jQuery.each(arr, function() {  // this 指定值
      $(“#” + this).text(“Mine is ” + this + “.”);  // this指向為數組的值, 如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

例子:———遍歷數組的項, 傳入index和value

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>
$.each( ['a','b','c'], function(i, l){
alert( “Index #” + i + “: ” + l );
});
 
</script>
</body>
</html>

例子:———遍歷對象的屬性,傳入 key和value

復制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>
 
$.each( { name: “John”, lang: “JS” }, function(k, v){
alert( “Key: ” + k + “, Value: ” + v );
});
 
</script>
</body>
</html>

正自評論的例子


復制代碼 代碼如下:

1. 如果不想輸出第一項 (使用retrun true)進入 下一遍歷
 
<!DOCTYPE html>
<html>
<head>
<script src=”http://code.jquery.com/jquery-latest.js”></script>
</head>
<body>
<script>
 
var myArray=["skipThis", "dothis", "andThis"];
$.each(myArray, function(index, value) {
if (index == 0) {
return true; // equivalent to ‘continue' with a normal for loop
}
// else do stuff…
alert (index + “: “+ value);
});
 
</script>
</body>
</html>

相關文章

最新評論

通海县| 北安市| 高陵县| 瓦房店市| 克山县| 荆门市| 嘉黎县| 汉沽区| 鞍山市| 宁城县| 阿克| 龙井市| 黄平县| 平泉县| 察哈| 南木林县| 兴安盟| 泊头市| 凤冈县| 长沙县| 长岭县| 巴楚县| 冕宁县| 嫩江县| 舟曲县| 奎屯市| 大英县| 阳原县| 苍溪县| 巴林右旗| 沿河| 巴青县| 靖宇县| 灌阳县| 两当县| 普陀区| 三门峡市| 融水| 色达县| 旬邑县| 山西省|