Home > Web Front-end > JS Tutorial > body text

How to traverse a list collection in jquery

王林
Release: 2020-11-30 11:49:55
Original
3280 people have browsed it

Jquery method of traversing a list collection: You can use the each method to traverse, such as [$.each(obj, function(n, value)]. The each method specifies the function to be run for each matching element.

How to traverse a list collection in jquery

The operating environment of this tutorial: windows10 system, jquery2.2.4 version, this method is suitable for all brands of computers.

(Learning video sharing: jquery video tutorial)

Method introduction:

each() method specifies the function to be run for each matching element.

Syntax:

$(selector).each(function(index,element))
Copy after login

Parameters:

function(index,element) Required. Specifies the function to be run for each matching element. index - the index position of the selector element - the current element ( You can also use the "this" selector)

The implementation code is as follows:

<script language="javascript" type="text/javascript " src="jquery.min.js "></script>
<script  type="text / javascript ">
$(function(){
var tbody = "";
//------------遍历List集合 .each的使用-------------
var obj =[{"name ":"项海军","password ":"123456 "},{"name ":"科比","password ":"333333 "}];
$("#result ").html("遍历List集合.each的使用");
alert(obj);//是个object元素
//下面使用each进行遍历
$.each(obj,function(n,value) {
alert(n+&#39; &#39;+value);
var trs = "";
trs += " < tr > <td > " +value.name+" < /td> <td>" + value.password +"</td > </tr>";
tbody += trs;
});
$("#project").append(tbody);
});
</script >
Copy after login

For more programming-related knowledge, please visit: Programming Learning!!

The above is the detailed content of How to traverse a list collection in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template