2011-01-13 58 views
6

如果URL中沒有散列存在,試圖讓#home顯示。我想沿着這些路線的東西會工作很容易,但我不能得到任何東西去:如果沒有散列,jquery散列

if(window.location.hash != null){ 
     $(window.location.hash).fadeIn(800); 
    } else { 
     $('#home').fadeIn(800); 
    } 

我從來沒有用,如果/ jQuery中else語句,所以這顯然是錯誤的

謝謝合作!

+0

將`window.location.hash`給`null`?也許你應該先在一些`JS API`中檢查。我認爲它會給你一個空的字符串。 – Marnix 2011-01-13 23:42:38

+0

@Marnix,你是對的。至少在Firefox中它會給出一個空字符串。 – 2011-01-13 23:50:53

回答

19

比較其是否爲空字符串,而不是(空和空字符串不等於在JavaScript):

if(window.location.hash != ''){ 
    $(window.location.hash).fadeIn(800); 
} else { 
    $('#home').fadeIn(800); 
}