jQuery 공작소 : :first

jQuery 공작소에 방문해 주셔서 감사드립니다. 쉬운 예제로 느껴보세요!!

:first 셀렉터로 선택된 요소의 첫번째것을 찾습니다.

예제 1
기본 예제
보기
제일 앞의 "CSS 공작소" 를 선택합니다.
CSS 공작소
CSS 공작소
CSS 공작소
소스
<style type="text/css">
    .css_test div {
        border-radius : 5px;
        border : 5px solid gray;
        margin : 5px;
        padding : 7px;
    }
</style>

제일 앞의 "CSS 공작소" 를 선택합니다.<br>
<button type="button" onclick="$('.css_test div:first').css('border-color','red')">눌러보세용</button>
<button type="button" onclick="$('.css_test div:first').css('border-color','')">다시하기</button>

<div class="css_test">
    <div>CSS 공작소</div>
    <div>CSS 공작소</div>
    <div>CSS 공작소</div>
</div>
관련 CSS
jQuery

$( '.css_test  div:first' )  -  클래스명이 css_test 인것의 div 중 첫번째 것을 찾습니다.
$( '.css_test  div:first' ).css( 'border-color', 'red' )  -  첫번째 div 의 테두리색을 빨간색으로 바꾸기.

예제 2
응용 예제
보기
맨 앞의 사람부터 내보내기
소스
<style type="text/css">
    .css_test {
        border-radius : 5px;
        border : 5px solid gray;
        padding : 7px;
    }
    .css_test img {
        position : relative;
    }
</style>

맨 앞의 사람부터 내보내기<br>
<button type="button" onclick="j_test()">나가세요</button>

<div class="css_test">
    <img src="//superkts.com/img/huk1.gif" />
    <img src="//superkts.com/img/huk1.gif" />
    <img src="//superkts.com/img/huk1.gif" />
    <img src="//superkts.com/img/huk1.gif" />
    <img src="//superkts.com/img/huk1.gif" />
    <img src="//superkts.com/img/huk1.gif" />
</div>

<script type="text/javascript">
    function j_test(){
        $('.css_test img:first').animate( {left:'-=150', 'opacity':0 }, 500, function(){
            $(this).remove();
        } );
    }
</script>
관련 CSS
jQuery

$( '.css_test  img:first' ).animate( { left : '-=150', 'opacity' : 0 }, 500, function(){
        $(this).remove();
} );

해당 셀렉터의 첫번째 이미지를 찾아서 왼쪽으로 150px 이동시킴과 동시에 투명해 지도록 만들고
애니메이션이 끝나면 해당 이미지를 삭제합니다.

관렴 메서드 : .animate(), .remove()

예제 3
번외) :first 셀렉터와 :first-child 의 차이점 보기
보기
버튼을 눌러보세용

소스
<style type="text/css">
    .css_test {
        background : #efefef;
        border-radius : 5px;
        border : 5px solid gray;
    }
    .css_test div {
        background : #fff;
        border-radius : 5px;
        margin : 10px;
        padding : 10px;
    }
</style>

버튼을 눌러보세용<br><br>
<button type="button" onclick="j_test_fc()">first-child 는 봅니다</button>
<button type="button" onclick="j_test_f()">first 는 봅니다</button>

<div class="css_test">
    <div>
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
    </div>
    <div>
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
    </div>
    <div>
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
        <img src="//superkts.com/img/huk1.gif" />
    </div>
</div>

<script type="text/javascript">
    function j_test_fc(){ // first-child
        $('.css_test div img:first-child').attr('src', '//superkts.com/img/huk2.gif');
        setTimeout(function(){
            // 0.5초후 원복
            $('.css_test img').attr('src', '//superkts.com/img/huk1.gif');
        }, 500);
    }

    function j_test_f(){ // first
        $('.css_test div img:first').attr('src', '//superkts.com/img/huk2.gif');
        setTimeout(function(){
            // 0.5초후 원복
            $('.css_test img').attr('src', '//superkts.com/img/huk1.gif');
        }, 500);
    }
</script>
관련 CSS
jQuery

$( '.css_test div img:first-child' )
- 클래스명이 css_test 인 요소의 div 안의 img첫번째 자식만 찾기
- 각 div 안에 있는 이미지중 첫번째 것들 즉 첫번째 자식만 찾습니다.
- 예제에서는 각 3개의 div 중 첫번째 이미지들이 대상입니다.
- 각 div 가 부모이고 그 안의 첫번째 것을 의미하죠.

$( '.css_test div img:first' )
- 클래스명이 css_test 인 요소의 div 안의 img첫번째 것만 찾기
- 찾아진 이미지를 전체로 놓고 판단하여 첫번째 것만 찾습니다.
- 무조건 첫번째 것만 찾는것이죠.

+ 관련 jQuery +
jQuery 홈페이지 :first API 문서 : http://api.jquery.com/first-selector/