2013-05-01 2 views
0

방금 ​​작은 if, else if 루프를 작성했습니다. 콘솔에 unexpected token ';' 오류가 표시됩니다. 각 $(this).css('top', top-3"em"); 진술 후 ;을 제거하면 else if이 예기치 않게 발생하기 전에 }이라는 또 다른 오류가 발생합니다.if else if loop broken

여기 내 루프가 있습니다. 내가 빗나 갔을지도 모르는 아이디어가 있습니까?

$('div.result').each(function() { 

    $(this).css('top', top+"em"); 
    top = top + 8; 

    resultcount = resultcount - 1; 

    if(resultcount=5) { 
     $(this).css('top', top-3"em"); 
    } else if(resultcount=4) { 
     $(this).css('top', top-7"em"); 
    } else if(resultcount=3) { 
     $(this).css('top', top-14"em"); 
    } else if(resultcount=2) { 
     $(this).css('top', top-20"em"); 
    } else(resultcount=1) { 
     $(this).css('top', top-30"em"); 
    } 

}); 
+0

유효하지 않은 비교에서 문자열 통합이 없음까지 일련의 구문 오류가 있습니다. – adeneo

+0

@ Borsel 당신은 ** If, else if loop broken **;)이라는 링크를 클릭 한 사람이기도합니다. –

답변

1

구문 오류가 적고 장소에 따라 수정해야 할 수도 있습니다.

  • 당신은 당신이 경우 마지막에 if 문을 놓친 할당 연산자 =

  • 대신 평등 연산자 ===를 사용해야합니다.

  • 당신은 될 문자열 top-3"em"을 연결하는 +를 사용할 필요가 (top-3)+"em"

이 구문 오류가 무료

Live Demo

$('div.result').each(function() { 

    $(this).css('top', top+"em"); 
    top = top + 8; 

    resultcount = resultcount - 1; 

    if(resultcount===5) { 
     $(this).css('top', (top-3)+"em"); 
    } else if(resultcount===4) { 
     $(this).css('top', (top-7)+"em"); 
    } else if(resultcount===3) { 
     $(this).css('top', (top-14)+"em"); 
    } else if(resultcount===2) { 
     $(this).css('top', (top-20)+"em"); 
    } else if(resultcount===1) { 
     $(this).css('top', top-30+"em"); 
    }  
}); 
+0

'==='를 사용하고'=='이 존재하지 않는 것을 권장하십시오. – jbabey

+0

@jbabey 나는 "* 척 * = 존재하지 않는다 *"에 대해 당신과 동의하지 않을 것이다. 그러나 ok ... –

+1

@jbabey 그냥 모든 javascript 답변에 추가하면 많은 친구를 만들지 않을거야. '== 0' 대신'=== 0'을 사용하는 것이 좋은 이유가 있지만'== 5' 대신'=== 5'를 사용하는 것이 많지 않습니다. – Blazemonger

3

당신은 RESULTCOUNT var에 설정되어, 당신은 그것을 확인되지 않습니다 결과와 종류를 확인하기위한

if(resultcount===5) { 
    $(this).css('top', "top-3em"); 
} 
else if(resultcount===4) { 
    $(this).css('top', "top-7em"); 
} 
else if(resultcount===3) { 
    $(this).css('top', "top-14em"); 
} 
else if(resultcount===2) { 
    $(this).css('top', top-20"em"); 
} 
else { 
    $(this).css('top', "top-30em"); 
} 

사용 ===을.

또한 많은 구문 오류가 있으므로 개발자 도구를 사용하여 디버깅 해보십시오.

+1

'==='을 사용하고'=='이 존재하지 않는 것을 권장합니다. – jbabey

+2

코드를 직접 실행 해 보셨습니까? – Blazemonger

+0

구문 오류가 가득 찼습니까? – adeneo

2

이 당신이 사용해야하는 시도이다 = = instea d 중 = 내부 if 그리고 각 CSS 매개 변수에 +를 추가하는 것을 잊었습니다.

$('div.result').each(function() { 

     $(this).css('top', top + " em"); 
     top = top + 8; 

     resultcount = resultcount - 1; 

     if(resultcount ==5) { 
      $(this).css('top', top-3 + "em"); 
     } else if(resultcount==4) { 
      $(this).css('top', top-7 + "em"); 
     } else if(resultcount==3) { 
      $(this).css('top', top-14 + "em"); 
     } else if(resultcount==2) { 
      $(this).css('top', top-20 + "em"); 
     } 

    }); 
+0

else (resultcount == 1)'else if'이어야합니다 – Blazemonger

+0

Whoop! 작품들 ('else' 대신에'else if'를 포함하여)'감사합니다! – lukeseager