Home  >  Article  >  Web Front-end  >  Jquery knowledge point 2: Operations on arrays under jquery_jquery

Jquery knowledge point 2: Operations on arrays under jquery_jquery

WBOY
WBOYOriginal
2016-05-16 18:12:06811browse

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

Copy code The code is as follows:

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
Copy code The code is as follows:

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
Copy code The code is as follows:

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:
Copy code The code is as follows:

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:
Copy code The code is as follows:

<%@ 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:
Copy code The code is as follows:



< ;head>







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,
Copy code The code is as follows:



;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 객체의 배열을 표시하려면 다음 코드도 위의 효과를 얻을 수 있습니다.



코드 복사 Jquery knowledge point 2: Operations on arrays under jquery_jquery

코드는 다음과 같습니다. 다음과 같습니다:
<제목>
<스크립트 유형 ="text/javascript " >
$(function() {
$("p").each(function() {
$(this).text("p 태그입니다.");
})
})

;

/본문>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn