테스트 사이트 - 개발 중인 베타 버전입니다

for 문 채택완료

아르르 5년 전 조회 2,800

 $( "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개

5년 전

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

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

 

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

아래처럼 해보세요

 

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

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

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

});

 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

플래토
5년 전

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();

}

 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인