jQuery / Property / .length - 선택한 요소의 개수를 반환하는 속성
.length
.length는 선택한 요소의 개수를 반환하는 속성입니다. 예를 들어
$( 'div' ).length
는 div 요소의 개수입니다.
예제
버튼을 클릭하면 li 요소의 개수를 출력합니다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>jQuery</title> <script src="//code.jquery.com/jquery-1.11.0.js"></script> <script> $( document ).ready( function() { $( 'button' ).click( function() { var n = $( 'li' ).length; $( 'ul' ).after( '<p>There are ' + n + ' lis.</p>' ); } ); } ); </script> </head> <body> <button>Click To Count</button> <ul> <li>Lorem</li> <li>Ipsum</li> <li>Dolor</li> </ul> </body> </html>