2017-02-09 98 views
-2

我有一張關於城市的信息表。我試圖在溫度低於32並且海拔高於1000時顯示圖像(在div內)。JQuery兩個if語句

我的聲明不斷出現錯誤。

$("td.condition").each(function(){ 
    if($("td.elevation").text() > 1000) && ($("td.high_temp").text() < 32)));  
    } 
    $(".ice").show(); 
}); 
+0

閱讀錯誤將是一個很好的第一步。這看起來像是一個奇怪的混合語法錯誤。你有一個空的'if'塊,然後關閉該函數,然後在關閉該函數後嘗試使用更多代碼,然後嘗試再次關閉該函數*。 – David

回答

1

這是一個很破的結構:

$("td.condition").each(function(){ 
    if($("td.elevation").text() > 1000) && ($("td.high_temp").text() < 32))); 
    // The above is an empty "if" because of the semi-colon after it. 
    // So it checks the condition, but then doesn't do anything. 
} 
// Now the anonymous function is closed. 
$(".ice").show(); 
// Which means the above line of code is trying to be passed as an argument to each(), which doesn't make sense. 
}); 
// Then you have a stray } and then close the call to each() 

如果以.show()該呼叫被認爲是if塊內,那麼你要放在一個大括號塊以下的if

$("td.condition").each(function(){ 
    if($("td.elevation").text() > 1000) && ($("td.high_temp").text() < 32))) { 
     $(".ice").show(); 
    } 
});