自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

jQuery入門:數(shù)組的三種類型三種操作

開發(fā) 前端
眾所周知,JQuery是對JavaScript的一種高效的封裝,所以jQuery要操作的數(shù)組即是JavaScript中的數(shù)組,在JavaScript中我們使用for以及for-in進行數(shù)組的操作,而在jQuery中則使用$.map()、$.each()來操作數(shù)組:

眾所周知,jQuery是對JavaScript的一種高效的封裝,所以jQuery要操作的數(shù)組即是JavaScript中的數(shù)組,在JavaScript中我們使用for以及for-in進行數(shù)組的操作,而在jQuery中則使用$.map()、$.each()來操作數(shù)組:

首先是普通的數(shù)組(索引為整數(shù)的數(shù)組):

  1. $.map(arr,fn); 

對數(shù)組中的每個元素調(diào)用fn函數(shù)逐個進行處理,fn函數(shù)將處理返回***得到的一個新的數(shù)組

  1. var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];  
  2.       var newarr = $.map(arr, function(item) {return item*2 });  
  3.       alert(newarr); 

還可以省略function的參數(shù),這個時候this可以得到遍歷的當(dāng)前元素的值

  1. var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];  
  2.         $.each(arr, function(key, value) { alert("key:" + key + "value:" + value); });  

然后是索引為字符串的 鍵值對數(shù)組,針對這類數(shù)組,

一般采用$.each(array,fn)來操作:

  1. var arr = { "jim""11""tom""12""lilei""13" };  
  2.      $.each(arr, function(key, value)
  3.  { alert("姓名:"+key+"年齡:"+value); }); 

當(dāng)然也可以使用無參的的function進行遍歷;

當(dāng)這類數(shù)據(jù)從服務(wù)器端獲取時可以如下進行:

服務(wù)器端:

  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2.  
  3. using System;  
  4. using System.Web;  
  5. using System.Web.Script.Serialization;  
  6. using System.Collections.Generic;  
  7. public class Handler : IHttpHandler {  
  8.       
  9.     public void ProcessRequest (HttpContext context) {  
  10.         context.Response.ContentType = "text/plain";  
  11.         Person p1 = new Person { Age = "22", Name = "tom" };  
  12.         Person p2 = new Person { Age = "23", Name = "jim" };  
  13.         Person p3 = new Person { Age = "24", Name = "lilei" };  
  14.         IList<Person> persons = new List<Person> {p1,p2,p3};  
  15.         JavaScriptSerializer js = new JavaScriptSerializer();  
  16.          string s= js.Serialize(persons);  
  17.         context.Response.Write(s);  
  18.     }  
  19.  
  20.     public class Person  
  21.     {  
  22.         public string Name { get; set; }  
  23.         public string Age { get; set; }  
  24.     }  
  25.     public bool IsReusable {  
  26.         get {  
  27.             return false;  
  28.         }  
  29.     }  
  30.  
  31. }  

先實例化了三個person對象,然后放到一個集合中,***把這個集合序列化成字符串流到客戶端;

客戶端:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head>  
  6.     <title></title>  
  7.  
  8.     <script src="../myjs/jquery-1.4.2.js" type="text/javascript"></script>  
  9.     <script  type ="text/javascript" >  
  10.         $.get("Handler.ashx"function(data) {  
  11.             var persons = $.parseJSON(data);  
  12.             $.each(persons, function(key, person) {   
  13.             alert("Age:"+person.Age+"Name:"+person.Name) });  
  14.         });  
  15.     </script>  
  16. </head>  
  17. <body>  
  18.  
  19. </body>  
  20. </html>  

客戶端通過$.parseJSON()將后臺傳遞過來的字符串轉(zhuǎn)化為js數(shù)組對象,接下來我們就使用操作普通數(shù)組的方式來操作這個得到的數(shù)組

第三種就是通過標(biāo)簽選擇器獲取的jQuery對象數(shù)組,

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head>  
  6.     <title></title>  
  7.  
  8.     <script src="../myjs/jquery-1.4.2.js" type="text/javascript"></script>  
  9.     <script  type ="text/javascript" >  
  10.         $(function() {  
  11.             $("p").text("這是p標(biāo)簽");  
  12.         });  
  13.     </script>  
  14. </head>  
  15. <body>  
  16.  <p></p>  
  17.   <p></p> <p></p> <p></p> <p></p>  
  18.   <p></p>  
  19. </body>  
  20. </html>  

在瀏覽器中運行的效果為:

jQuery


在dom加載完成后為每一個p元素動態(tài)的添加了文本,首先$("p")獲取p標(biāo)簽的集合,相當(dāng)于JavaScript中的document.getElementByTagName只是這里得到的是jQuery對象的數(shù)組,這樣就有了jQuery固有的隱式迭代的功能,后面的text("這是p標(biāo)簽")的操作就迭代到了每一個P標(biāo)簽上,我們也可以顯示的調(diào)用each函數(shù)來顯示的迭代獲得的jQuery對象數(shù)組,顯示的調(diào)用each可以看作是對$.each()的簡化調(diào)用,下面的代碼同樣可以實現(xiàn)上面的效果:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head>  
  6.     <title></title>  
  7.  
  8.     <script src="../myjs/jquery-1.4.2.js" type="text/javascript"></script>  
  9.     <script  type ="text/javascript" >  
  10.         $(function() {  
  11.             $("p").each(function() {  
  12.             $(this).text("這是p標(biāo)簽");  
  13.             });  
  14.         });  
  15.     </script>  
  16. </head>  
  17. <body>  
  18.  <p></p>  
  19.   <p></p> <p></p> <p></p> <p></p>  
  20.   <p></p>  
  21. </body>  
  22. </html> 

原文鏈接:http://www.cnblogs.com/xieLongBao/archive/2011/01/14/1935920.html

【編輯推薦】

  1. PHP+XML+jQuery實現(xiàn)即時功能
  2. 犀利開發(fā)——jQuery內(nèi)核詳解與實踐
  3. jQuery數(shù)組處理完全詳解(實例演示)
責(zé)任編輯:陳貽新 來源: 謝龍寶的博客
相關(guān)推薦

2022-06-20 08:50:16

TypeScript類型語法

2009-06-29 18:21:29

Hibernate

2010-05-11 14:08:50

MySQL數(shù)字類型

2018-12-13 20:14:18

物聯(lián)網(wǎng)平臺物聯(lián)網(wǎng)IOT

2009-11-24 18:15:37

博科資訊管理軟件

2009-08-03 17:41:20

ASP.NET Cac

2009-11-13 09:39:48

2010-04-12 16:35:15

Oracle數(shù)據(jù)庫

2010-11-01 11:57:18

DB2客戶端

2013-09-02 15:35:00

2023-10-13 00:00:00

Redis模塊空間對象

2010-04-02 13:15:01

Oracle跟蹤

2020-05-08 07:26:16

物聯(lián)網(wǎng)平臺物聯(lián)網(wǎng)IOT

2010-09-24 19:18:22

SQL索引

2010-09-09 10:43:56

VPN服務(wù)

2009-08-06 15:26:18

C#異常類型

2010-10-28 10:27:35

oracle賦權(quán)

2010-09-25 14:38:29

SQL分頁

2009-07-16 16:23:59

Swing線程

2018-03-28 16:10:23

閱讀源碼境界
點贊
收藏

51CTO技術(shù)棧公眾號