Home  >  Article  >  Web Front-end  >  How to traverse a list collection in jquery

How to traverse a list collection in jquery

王林
王林Original
2020-11-26 09:26:033251browse

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))

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 >

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!

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