Home > Article > Web Front-end > How to get how many li there are in jquery
Jquery method to get how many li there are: 1. Use the "$('li')" statement to get the li object; 2. Use the length attribute to calculate the length of the obtained li object, and you can get the li in the document. The number of elements, the syntax is "li object.length;".
The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.
How to get how many li's there are in jquery
1. Create a new html file and name it test.html to explain how to get it in jquery The number of li under ul. Use the ul tag to create a list of items for testing. Add a class attribute to the ul tag to obtain the ul object.
Use the button tag to create a button, bind the onclick event to the button, and when the button is clicked, execute the getLength() function.
2. Create the getLength() function. Within the function, obtain the li object through class, use the length attribute to obtain the number of li, and use the alert() method to obtain the li object. Number output.
Open the test.html file in the browser, click the button to view the results.
Summary:
1. Use the ul tag to create a list of items, and add a class attribute to the ul tag to obtain the ul object.
2. Use the button tag to create a button, bind the onclick event to the button, and when the button is clicked, execute the getLength() function.
3. In the js tag, create the getLength() function. Within the function, obtain the li object through class, use the length attribute to obtain the number of li, and use the alert() method to output the obtained number.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of How to get how many li there are in jquery. For more information, please follow other related articles on the PHP Chinese website!