for 문

for 문

QA

for 문

답변 2

본문

 $( "div" ).each( function() {
         if ( $( 'li', this ).length == 2 ) {
             $( this ).addClass( 'item2' );
        } else if( $( 'li', this ).length == 3) {
             $( this ).addClass( 'item3' );
        }else if( $( 'li', this ).length == 4) {
             $( this ).addClass( 'item4' );
        }else if( $( 'li', this ).length == 5) {
             $( this ).addClass( 'item5' );
        }else if( $( 'li', this ).length == 6) {
             $( this ).addClass( 'item6' );
        }
});

 

 

위 코드 for문으로 10번 돌리는 법

 

답변 좀 부탁 드릴게요.....

이 질문에 댓글 쓰기 :

답변 2

function divLoops() {

   $( "div" ).each( function() {
         if ( $( 'li', this ).length == 2 ) {
             $( this ).addClass( 'item2' );
        } else if( $( 'li', this ).length == 3) {
             $( this ).addClass( 'item3' );
        }else if( $( 'li', this ).length == 4) {
             $( this ).addClass( 'item4' );
        }else if( $( 'li', this ).length == 5) {
             $( this ).addClass( 'item5' );
        }else if( $( 'li', this ).length == 6) {
             $( this ).addClass( 'item6' );
        }
   });

}

 

for (var i = 0; i < 10; i++) {

    divLoops();

}

 

 for문으로 10번 ??? 무엇을 하려는지 모르겠군요

div가 10개있으면 저절로 10번 실행될텐데요

 

그리고 저렇게 규칙적인 것은 한번만으로 될텐요

아래처럼 해보세요

 

$( "div" ).each( function() {

 nn = $( 'li', this ).length; //<--이것은 무엇을 하려는 의도인가요? li의 갯수 라면 $(this).find('li').length; 

$( this ).addClass( 'item' +nn);

});

 

답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 0
© SIRSOFT
현재 페이지 제일 처음으로