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

How to get the length of an array in jquery

coldplay.xixi
Release: 2023-01-04 09:36:52
Original
4190 people have browsed it

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].

How to get the length of an array in jquery

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.

Example:

<%@ 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"];
                $(&#39;h2&#39;).text(&#39;Members of my group: &#39;);
                $(&#39;p&#39;).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 = $(&#39;li&#39;).get();
                alert("获得了数组,数组长度为:" + names.length);
            });
        </script>
    </head>
    <body>
        <h2>Hello</h2>
        <p></p>
        <ol id="list">
        </ol>
    </body>
</html>
Copy after login

2, Old version

In the old version of jquery, you can use the

size() method to get the size of the array length. (The new version of JQ has removed the size method)

The usage method is as follows:

console.log($("li").size());
Copy after login

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!

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