Home >Web Front-end >JS Tutorial >How to get the length of an array in jquery
How to get the length of an array in jquery: You can use the length attribute to get the length of the array. The length attribute contains the number of elements in the jQuery object. The attribute syntax is [$(selector).length].
The operating environment of this tutorial: windows7 system, jquery3.2.1 version, thinkpad t480 computer.
Recommended: jquery video tutorial
##Method to get the array length in jquery:
1. In the new version of
jquery, you can use the length attribute to get the length of the array. The length attribute contains the number of elements in the jQuery object. The attribute syntax is$(selector).length.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>显示数组中的各个元素</title> <link rel="stylesheet" href="common.css" type="text/css" /> <script src="jquery-1.5.2.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready( function() { var members = ["Mickey", "William", "Henry"]; $('h2').text('Members of my group: '); $('p').html(members.join("<br />")); var memberlist = $("#list"); $.each(members, function(index, value){ //append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 memberlist.append(index + " : " + value ); memberlist.append("<li>"+value+"</li>"); }); //下面利用jQuery获取li中内容的数组,并计算数组长度 var names = $('li').get(); alert("获得了数组,数组长度为:" + names.length); }); </script> </head> <body> <h2>Hello</h2> <p></p> <ol id="list"> </ol> </body> </html>
2, Old version
In the old version of jquery, you can use thesize() method to get the size of the array length. (The new version of JQ has removed the size method)
console.log($("li").size());
Related free learning recommendations: javascript (video)
The above is the detailed content of How to get the length of an array in jquery. For more information, please follow other related articles on the PHP Chinese website!