The length property usage instance of the jQuery object

  • 2020-05-09 18:12:09
  • OfStack

This example demonstrates the use of length properties for jQuery objects. Share with you for your reference. The specific analysis is as follows:

This property returns the number of objects in the matched collection of jQuery objects.

The length property returns the same value as the size() method.

Grammatical structure:

$("selector").length

Example code:


<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="//www.ofstack.com/" />
<title> The home of the script </title>
<style type="text/css">
span{color:red;}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#btn").click(function(){
    $("span").text($("li").length);
  })
});
</script>
</head>
<body>
<div>
  <ul>
    <li> The background section </li>
    <li> Zone at the front desk </li>
    <li> Database zone </li>
    <li> Master communication </li>
  </ul>
</div>
<div>li In the set of elements li Number of elements :<span></span></div>
<button id="btn"> Click to view an example </button>
</body>
</html>

The above code can get the number of li elements in the collection of li elements through the length attribute.

I hope this article has helped you with your jQuery programming.


Related articles: