The first is an ordinary array (an array with an index of integers):
$.map(arr,fn);
Call the fn function on each element in the array to process it one by one. The fn function will process and return the final result. A new array
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) calls the fn function to process each element of the array, and there is no return value
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each(arr, function (key, value) { alert("key:" key "value:" value); });
You can also omit the parameters of function. At this time, this can get the value of the current element traversed
var arr = [9, 8, 7, 6, 5, 4, 3, 2, 1];
$.each(arr, function() { alert(this); });
Then the key value indexed as a string For arrays,
generally uses $.each(array,fn) to operate:
var arr = { "jim": "11", "tom": "12", "lilei": "13" };
$.each(arr, function(key, value) { alert("Name:" key "Age:" value); });
Of course, you can also use a function without parameters to traverse;
When this Class data can be obtained from the server side as follows:
Server side:
<%@ 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
persons = new List {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;
}
}
}
First Three person objects are instantiated, then put into a collection, and finally the collection is serialized into a string and streamed to the client;
Client:
The client passes $.parseJSON() to the background The passed string is converted into a js array object. Next, we use the method of operating ordinary arrays to operate the obtained array
The third is the Jquery object array obtained through the tag selector,
;script src="../myjs/jquery-1.4.2.js" type="text/javascript">
🎜 ><본문>
p>
browser For:
dom이 로드된 후 텍스트가 각 p 요소에 동적으로 추가됩니다. 먼저 $("p")는 document.getElementByTagName에 해당하는 p 태그 컬렉션을 가져옵니다. Javascript이지만 여기서 얻은 것은 Jquery 객체의 배열이므로 Jquery는 고유한 암시적 반복 함수를 갖습니다. 후속 텍스트("This is the p tag") 작업은 각 P 태그를 명시적으로 호출할 수도 있습니다. 반복을 통해 얻은 Jquery 객체의 배열을 표시하려면 다음 코드도 위의 효과를 얻을 수 있습니다.
코드 복사
코드는 다음과 같습니다. 다음과 같습니다:
<스크립트 유형 ="text/javascript " >
$(function() {
$("p").each(function() {
$(this).text("p 태그입니다.");
})
})
;
/본문>