for 문

for 문

QA

for 문

본문

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

});

 

답변을 작성하시기 전에 로그인 해주세요.
전체 78
QA 내용 검색
filter #for ×

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT