[jQuery] jQuery length 체크

    [jQuery] jQuery length 체크

     

    <!doctype html>
    <html lang="ko">
      <head>
        <meta charset="utf-8">
        <title>jQuery lengh</title>
        <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
        <script>
          $( document ).ready( function() {
            $( 'button' ).click( function() {
              var n = $( 'li' ).length;
              $( 'ul' ).after( '<p>There are ' + n + ' length.</p>' );
            } );
          } );
        </script>
      </head>
      <body>
        <button>클릭</button>
        <ul>
          <li>1</li>
          <li>2</li>
          <li>3</li>
        </ul>
      </body>
    </html>

     

    위 HTML을 실행 시 아래와 같이 나오게 되는데 클릭을 누르게 되면 3이라고 LENGTH가 나오게 된다.

     

    var n = $( 'li' ).length;
    
         $( 'ul' ).after( '<p>There are ' + n + ' length.</p>' );

    li의 길이를 구해서 n에 넣게 되어 length 3이 나오게 된다. $(' li ').length 로 구하면 된다.

     

    jQuery length 체크

     

    '코딩 > jQuery' 카테고리의 다른 글

    [jQuery] Uncaught ReferenceError: $ is not defined 해결방법  (0) 2020.12.30

    댓글

    Designed by JB FACTORY