2010-12-19 51 views
17
jQuery(document).ready(function(){ 
    $("#red-products").hide(); 
    $("#content-info").click(function(event){ 
     $("#red-products").hide(); 
     $("#red-information").show(); 
    }); 

    $("#content-product").click(function(event){ 
     $("#red-information").hide(); 
     $("#red-products").show(); 
    }); 

    $("#more").click(function(event){ 
     load(this.href); 
     return false; 
    }); 

}); 

如您所見,默認情況下,#red-products已隱藏,#red-information可見。有時候我想#red-products可見和#red-information隱藏的,這意味着像使用jQuery獲取#anchor的URL?

http://localhost/networks2/profile.php?id=1&offset=1#products

顯示#red-products和隱藏#red-information。而

http://localhost/networks2/profile.php?id=1&offset=1#information

隱藏#red-products並顯示#red-information

如何使用jQuery從URL讀取錨點並隱藏/顯示適當的部分?

回答

36

您可以更改初始隱藏是基於window.location.hash,通過更換此:

$("#red-products").hide(); 

有了這個:

$("#red-products, #red-information").hide(); 
$("#red-" + (window.location.hash.replace("#", "") || "information")).show(); 

這將隱藏兩個最初,然後顯示hasd(#red-hashhere) ,或者默認爲現在顯示#red-information

+0

我不知道它不起作用:| – 2010-12-19 14:25:29

+0

@亞當 - 這不是一個非常有用的描述:)什麼是不工作......你是否正確地使用網址,就像你在問題中一樣? – 2010-12-19 14:27:28

+0

ups位置mising h:D非常感謝matee! – 2010-12-19 14:28:12