• [제이쿼리]$('a,b')와 $('a','b')의 차이점

    2021. 2. 23.

    by. 웰시코더

    반응형

    최근 프로젝트에서 $('a', 'b') 문법이 종종 나오고 있는데 해석할 때마다 헷깔릴 때가 있어서 정리한다.

     

    $('a','b')라고 쓰면 b개체에 포함된 a개체를 선택한다는 의미이다.

     

    $('a, b')로 쓰면 a와 b 개체를 모두 포함한다.

     

     

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <script src="https://code.jquery.com/jquery-2.2.1.js"></script>
        </head>
        <body>
            <table class="table">
                <thead>
                    <th>head1</th>
                    <th>head2</th>
                </thead>
                <tbody>
                    <tr>
                        <td class="data1" id="id1" >headData1</td>
                    </tr>
                    <tr>
                        <td class="data2" id="id2">headData2</td>
                    </tr>
                </tbody>
            </table>
        </body>
    </html>
    
    <script>
        console.log($('.data1', '.table').attr('id'));      //id1 
        console.log($('.table .data1').attr('id'));         //id1
        console.log($('.data1, .data2').size());            //2
        console.log($('.data1, .data2').eq(1).attr('id'));  //id2
    </script>

     

     

     

    반응형

    댓글