2015-10-02 45 views
0

我的代碼中的If - Else語句似乎在導致錯誤。點擊圖片時,如果圖片尺寸爲100px×100px,並且在任何其他情況下打開了雅虎窗口,我都會嘗試打開Goog​​le窗口。任何想法是什麼使if語句出錯?爲什麼我的if語句在jquery中檢測不到圖像大小?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script type="text/javascript"> 


$(document).ready(function() { 
$("#testImgID").click(function(){ 

    var imgWidth = $(this).width(); 
    var imgHeight = $(this).height(); 

    if(imgWidth == 100 && imgHeight == 100) 
    { 
     window.open("http://www.google.com"); 
    else 
    { 
     window.open("http://www.yahoo.com"); 
    } 
}); 
}); 

<p> 
<img width="100" height="100" id="testImgID" alt="Setup client email capture.jpg" src="/wg/PayrollSolutions/PublishingImages/RCP_setups_theatrical_assignedTo_table.png" style="margin: 5px"/> 
</p> 

回答

1

你有一個語法錯誤,你沒有關閉第一,如果

$(document).ready(function() { 
    $("#testImgID").click(function() { 

     var imgWidth = $(this).width(); 
     var imgHeight = $(this).height(); 

     if (imgWidth == 100 && imgHeight == 100) { 
      window.open("http://www.google.com"); 
     } else { // } was missing 
      window.open("http://www.yahoo.com"); 
     } 
    }); 
}); 

,以檢查是否有語法錯誤,請務必檢查開發者控制檯(F12通常情況下),你應該看到:Uncaught SyntaxError: Unexpected token else以及錯誤在哪一行。

+0

謝謝馬科斯!我沒有意識到if-else語句需要兩個右括號。 – shampouya

+1

如果你打開一個支架,你必須始終關閉它;)他們成對:) –

相關問題