$( "li" ).eq( 2 ).css( "background-color", "red" ); // index 2번
$( "li" ).eq( -2 ).css( "background-color", "red" ); // 뒤에서 두번 째 (index X)
$( "body" ).find( "div" ).eq( 2 ).addClass( "blue" ); // 사용방법
$( "li" ).even().css( "background-color", "red" );
$( "ul li" ).even().addClass( "highlight" );
$( "li" ).odd().css( "background-color", "red" );
$( "li" ).filter( ":nth-child(2n)" ).css( "background-color", "red" );
$( "div" ).css( "background", "#c8ebcc" ).filter( ".middle" ).css( "border-color", "red" );
$( "li" )
.filter(function( index ) {
return index % 3 === 2;
})
.css( "background-color", "red" );
$( "li" ).first().css( "background-color", "red" );
$( "ul li" ).last().addClass( "highlight" );
Reduce the set of matched elements to the first in the set.
$( "li" ).has( "ul" ).css( "background-color", "red" );
//2-depth ul을 가지고있는 li찾을 수 있음.
$( "div" ).one( "click", function() {
if ( $( this ).is( ":first-child" ) ) {
$( "p" ).text( "It's the first div." );
} else if ( $( this ).is( ".blue,.red" ) ) {
$( "p" ).text( "It's a blue or red div." );
} else if ( $( this ).is( ":contains('Peter')" ) ) {
$( "p" ).text( "It's Peter!" );
} else {
$( "p" ).html( "It's nothing <em>special</em>." );
}
$( "p" )
.append( $( "input" ).map(function() {return $( this ).val();})
.get()
.join( ", " ) );
$( "div" ).not( ".green, #blueone" )
.css( "border-color", "red" );
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
$( "li" ).slice( 2 ).css( "background-color", "red" );
//index 2번 부터 빨간색으로 변경 (세번째)
$( "li" ).slice( 2, 4 ).css( "background-color", "red" );
//index 2 ~ 4번, 단 4번은 포함안되어서 세번째, 네번째 엘리먼트만 빨간색으로 변경
( "li" ).slice( -2, -1 ).css( "background-color", "red" );
// 뒤에서 2번째에서 시작, 1번째에서 마감이기 때문에 뒤에서 두번째 요소만 변경됨.