2013-08-06 95 views
0


我使用以下腳本在我的網頁上打開/關閉div;顯示帶錨點URL的切換div

$(document).ready(function(){ 

    $(".showA").hide(); 
    $(".show_hideB").show(); 

    $('.show_hideB').click(function(){ 
    $(".showA").slideToggle(); 

    }); 

}); 

一切都很正常,但問題是,我不能讓它開始工作,當我衝浪www.websitelink.com/#A到(切換)打開錨DIV #A自動

我已經嘗試過以下,我發現這裏但didnt幫了我:

if(document.location.hash) { 
$('.' + document.location.hash.substring(1)).slideDown(); } 

注:我使用JavaScript以更多的方式,所以我正在尋找一個通用的解決方案的切換和不只針對#A div(例如;如果網址是:www.websitelink.com#c它應該打開#c ect。等)

我希望你們中有人能爲我解決問題!
先進的謝謝!

+0

我不如果你的頁面上有一個CSS類名與所提供的'document.location.hash'相對應,並且你的代碼開始'if(document.location.hash)'後被調用,那麼你的代碼沒有看到問題文檔已經加載(例如在'$( document).ready'函數) – getsetcode

回答

0

你在選擇使用.,它應該是#

if(document.location.hash) { 
    $('#' + document.location.hash.substring(1)).slideDown(); 
} 

或者,由於document.location.hash總是#開始,你不需要子:

if(document.location.hash) { 
    $(document.location.hash).slideDown(); 
} 
+0

您需要將代碼放在'ready()'處理程序中。 – Barmar

+0

做到了!謝謝 :) –