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

Jquery中對(duì)數(shù)組的操作代碼

 更新時(shí)間:2011年08月12日 11:30:45   作者:  
眾所周知,Jquery是對(duì)JavaScript的一種高效的封裝,所以Jquery要操作的數(shù)組即是JavaScript中的數(shù)組,在 JavaScript中我們使用for以及for-in進(jìn)行數(shù)組的操作
而在Jquery中則使用$.map()、$.each()來操作數(shù)組:
首先是普通的數(shù)組(索引為整數(shù)的數(shù)組):
復(fù)制代碼 代碼如下:

$.map(arr,fn);
對(duì)數(shù)組中的每個(gè)元素調(diào)用fn函數(shù)逐個(gè)進(jìn)行處理,fn函數(shù)將處理返回最后得到的一個(gè)新的數(shù)組
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
var newarr = $.map(arr, function(item) {return item*2 });
alert(newarr);
$.each(array,fn)對(duì)數(shù)組array每個(gè)元素調(diào)用fn函數(shù)進(jìn)行處理,沒有返回值
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each(arr, function(key, value) { alert("key:" + key + "value:" + value); });
還可以省略function的參數(shù),這個(gè)時(shí)候this可以得到遍歷的當(dāng)前元素的值
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each(arr, function() { alert(this); });

然后是索引為字符串的 鍵值對(duì)數(shù)組,針對(duì)這類數(shù)組,
一般采用$.each(array,fn)來操作:
var arr = { "jim": "11", "tom": "12", "lilei": "13" };
$.each(arr, function(key, value) { alert("姓名:"+key+"年齡:"+value); });
當(dāng)然也可以使用無參的的function進(jìn)行遍歷;
當(dāng)這類數(shù)據(jù)從服務(wù)器端獲取時(shí)可以如下進(jìn)行:
服務(wù)器端:
復(fù)制代碼 代碼如下:

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
Person p1 = new Person { Age = "22", Name = "tom" };
Person p2 = new Person { Age = "23", Name = "jim" };
Person p3 = new Person { Age = "24", Name = "lilei" };
IList<Person> persons = new List<Person> {p1,p2,p3};
JavaScriptSerializer js = new JavaScriptSerializer();
string s= js.Serialize(persons);
context.Response.Write(s);
}
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
}
public bool IsReusable {
get {
return false;
}
}
}

先實(shí)例化了三個(gè)person對(duì)象,然后放到一個(gè)集合中,最后把這個(gè)集合序列化成字符串流到客戶端;
客戶端:
客戶端通過$.parseJSON()將后臺(tái)傳遞過來的字符串轉(zhuǎn)化為js數(shù)組對(duì)象,接下來我們就使用操作普通數(shù)組的方式來操作這個(gè)得到的數(shù)組
第三種就是通過標(biāo)簽選擇器獲取的Jquery對(duì)象數(shù)組,
復(fù)制代碼 代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="../myjs/jquery-1.4.2.js" type="text/javascript"></script>
<script type ="text/javascript" >
$(function() {
$("p").text("這是p標(biāo)簽");
});
</script>
</head>
<body>
<p></p>
<p></p> <p></p> <p></p> <p></p>
<p></p>
</body>
</html>

在瀏覽器中運(yùn)行的效果為:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="../myjs/jquery-1.4.2.js" type="text/javascript"></script>
<script type ="text/javascript" >
$(function() {
$("p").each(function() {
$(this).text("這是p標(biāo)簽");
});
});
</script>
</head>
<body>
<p></p>
<p></p> <p></p> <p></p> <p></p>
<p></p>
</body>
</html>

相關(guān)文章

最新評(píng)論