2012-08-23 2 views
4

마지막 else if 문이 실행되지 않는 이유가 궁금합니다. 나는 이것을하려고 노력하고있다 :jQuery 창 너비 else if 문

$(document).ready(function() { 
    function checkWidth() { 
     var windowSize = $(window).width(); 

     if (windowSize <= 479) { 
      console.log("screen width is less than 480"); 
     } 
     else if (windowSize = 480 && windowSize <= 719) { 
      console.log("screen width is less than 720 but greater than or equal to 480"); 
     } 
     else if (windowSize = 720 && windowSize <= 959) { 
      console.log("screen width is less than 960 but greater than or equal to 720"); 
     } 
     else if (windowSize >= 960) { 
      console.log("screen width is greater than or equal to 960"); 
     } 
    } 

    // Execute on load 
    checkWidth(); 
    // Bind event listener 
    $(window).resize(checkWidth); 
});​ 

마지막 else if를 제외하고 모든 것이 콘솔에 기록된다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

감사합니다,

업데이트 : 내가 발견 한 아래

http://wicky.nillia.ms/enquire.js/

손 가장 좋은 방법 : 사람들을위한

여전히 관심, 나는 높은 enquire.js 플러그인을 추천합니다 JS에서 미디어 쿼리를 인식합니다.

+0

다른 경우/그렇다면 충분히 큰 화면이 있습니까? ... – adeneo

+0

예 960 픽셀보다 넓은 화면이 있습니다. 나는 console.log (windowSize) 할 수 있고 올바른 측정을 얻을 수있다. 두 개의 else if 문 (> = 960)을 제거하면 마지막 문이 올바르게 실행됩니다. 이것이 왜 그들 모두와 관련이 없는지 이해하도록 도와주세요. – beefchimi

+0

두 번째 연산자를 사용하지 않을 때마다 단일'='를 이중'=='으로 변경해야합니다. –

답변

16

= 코드에서 누락되었습니다. 그리고 windowSize는 비교되지 않지만 windowSize = 480 같은 결과로 새 값이 할당됩니다. 대신에이 버전을보십시오 : 당신은 더 큰 기호보다 누락하고

$(document).ready(function() { 
    function checkWidth() { 
     var windowSize = $(window).width(); 

     if (windowSize <= 479) { 
      console.log("screen width is less than 480"); 
     } 
     else if (windowSize <= 719) { 
      console.log("screen width is less than 720 but greater than or equal to 480"); 
     } 
     else if (windowSize <= 959) { 
      console.log("screen width is less than 960 but greater than or equal to 720"); 
     } 
     else if (windowSize >= 960) { 
      console.log("screen width is greater than or equal to 960"); 
     } 
    } 

    // Execute on load 
    checkWidth(); 
    // Bind event listener 
    $(window).resize(checkWidth); 
});​ 
+0

"windowSize> = 480"비트가 필요하지 않습니다. 이전 if에서 이미 알고 있기 때문입니다. –

+0

@shhac yup, 업데이트 됨. – Mahn

+0

정말 고마워요. 공헌 한 모든 사람. 나는 내 실수를 지금 깨닫는다. 도움에 정말 감사드립니다. 최대한 빨리 정답으로 받아 들일 것입니다. (여러분 모두 빠릅니다!) – beefchimi

2

:

else if (windowSize = 720 

하고 그냥 등호를 사용하고 계십니까?

보십시오이 대신은 :

$(document).ready(function() { 
    function checkWidth() { 
     var windowSize = $(window).width(); 

     if (windowSize < 480) { 
      console.log("screen width is less than 480"); 
     } 
     else if (windowSize < 720) { 
      console.log("screen width is less than 720 but greater than or equal to 480"); 
     } 
     else if (windowSize < 960) { 
      console.log("screen width is less than 960 but greater than or equal to 720"); 
     } 
     else { 
      console.log("screen width is greater than or equal to 960"); 
     } 
    } 

    // Execute on load 
    checkWidth(); 
    // Bind event listener 
    $(window).resize(checkWidth); 
});​ 

FIDDLE

+0

화면 크기가 정확히 480,720 또는 960 인 경우 작업을 수행 할 수 없으므로 작동하지 않습니다. –

+0

@ NicholasCardot - 여전히 편집 중이십니까? – adeneo

+0

우연히 그 아래의 코멘트 버튼 대신에 편집 버튼을 눌렀습니다. 방금 백스 페이스를 누르고 코멘트 버튼을 클릭했습니다. 잠궈 버렸어? –

2

이 때문에 당신의 다른 경우 문장들입니다. 값을 지정하는 등호 하나를 사용하여 확인하고 있습니다.

if (windowSize = 480 && windowSize <= 719) 

당신이

if (windowSize == 480 && windowSize <= 719) 

또는 일을해야> =, 그 의도 논리가 있다면.