Home > Web Front-end > Front-end Q&A > How does jquery determine how many li there are in ul?

How does jquery determine how many li there are in ul?

WBOY
Release: 2022-06-09 10:41:56
Original
2468 people have browsed it

Jquery determines how many li there are in ul: 1. Get the li element object under the specified ul, the syntax is "$("ul li")"; 2. Use the length attribute to get the li element object Quantity, the function of this attribute is to obtain the number of elements contained in the jQuery object. The syntax is "li element object.length".

How does jquery determine how many li there are in ul?

The operating environment of this tutorial: windows10 system, jquery3.4.1 version, Dell G3 computer.

How does jquery determine how many li there are in ul

1. Get the li element object

The syntax is:

$("ul li")
Copy after login

2. Get the number based on the element object

The length property contains the number of elements in the jQuery object.

Syntax

$(selector).length
Copy after login

The example is as follows:





123

<script>
$(document).ready(function(){
$("button").click(function(){
alert($(&quot;ul li&quot;).length);
});
});
</script>

  • Coffee
  • Milk
  • Soda
Copy after login

Output result:

How does jquery determine how many li there are in ul?

After clicking the button:

How does jquery determine how many li there are in ul?

Video tutorial recommendation: jQuery video tutorial

The above is the detailed content of How does jquery determine how many li there are in ul?. 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