我試圖爲谷歌地圖infowindow生成一些HTML內容。如果它們不等於null,undefined或「」(空字符串),我應該顯示7個值。如果語句條件檢查!=「undefined」失敗
但是,顯然當Property
爲undefined
時,我的if(e.Property != null || e.Property != "undefined" || e.Property == "")
不起作用。大部分情況是e.Email
未定義。因此,我的代碼不是跳過那部分,而是插入html + "<br />
部分。當我的電子郵件返回時,它會返回undefined
,如果是這種情況,它應該捕獲並跳過。
我試過寫作if(typeof e.Property != null || typeof e.Property != "undefined" || typeof e.Property == "")
,但這沒有什麼區別。
// 'e ' is JSON object
var generateHTML = {
init: function(e) {
if (e != null || e != "undefined"){
generateHTML.check(e);
}
},
check: function (e) {
if(e.Title != null || e.Title != "undefined" || e.Title == ""){
html = html + "<b>"+e.Title+"</b>";
}
if(e.Address != null || e.Address != "undefined" || e.Address == ""){
html = html +"<br />"+ e.Address;
}
if(e.Zipcode != null || e.Zipcode != "undefined" || e.Zipcode == ""){
html = html +"<br />"+ e.Zipcode+", ";
}
if(e.City != null || e.City != "undefined" || e.City == ""){
html = html + e.City;
}
if(e.Phone != null || e.Phone != "undefined" || e.Phone == ""){
html = html +"<br />"+ e.Phone;
}
if(e.Email != null || e.Email != "undefined" || e.Email == ""){
html = html +"<br />"+ e.Email;
}
if(e.WebAddress != null || e.WebAddress != "undefined" || e.WebAddress == ""){
html = html +"<br />"+ e.WebAddress;
}
return html;
}
};
你爲什麼把'undefined'成字符串? – Jon
可能重複[如何檢查未定義在JavaScript?](http://stackoverflow.com/questions/2985771/how-to-check-for-undefined-in-javascript) – Dennis
+1爲好問題 –