Blog Content

    티스토리 뷰

    21.jQuery - Each() 메서드 ( 01.Core-01.Each )

    반응형
    -Each() : 순차적으로 특정 함수를 수행 가능한 메서드

    반복문으로 jQuery에서 많이 사용됩니다. 이유는 있는 만큼 반복한다는 장점과

    Index에서 자동으로 반복 횟수가 저장됩니다.
    01.Each.htm

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>Each() 요소만큼 반복해서 속성 설정 가져오기</title>   

        <script src="../jQuery/jquery-1.3.2-vsdoc2.js" type="text/javascript"></script>

        <script type="text/javascript">

            $(document).ready(function() {

            //[1] each() 메서드 설명

            //$('p').each( function (index) { alert(index); });


            //[2] for문처럼 반복한다. index 0부터 자동 증가됨

                $('p').each(//p 태그 만큼 반복

                function(index) {

                    $(this).attr({    // attr({어트리뷰트:});

                        'id': "para-" + index //ID 속성 동적으로 부여

                    });

                });

                //[3] 0번째 요소의 텍스트 읽어오기

                $('#btn').click(function() {           

                    alert($('#para-2').text()); //text() : 태그안의 : ASP.NET

                });

            });

        </script>

     

    </head>

    <body>

     

        <p>C#</p>

        <p>ASP.NET</p>

        <p>Silverlight</p>

        <input type="button" id="btn" value="동적으로 생성된 id 개체 접근" />

       

    </body>

    </html>


    결과화면

    [그림21-1]



    반응형

    Comments