2011-03-25 67 views
1

我在網站上工作,客戶希望能夠訪問網站上每個其他頁面的服務頁面上的動態內容,但下面的代碼使其顯示第一個列表中的「div」,而不是被點擊的錨。從另一個頁面訪問動態內容

$(document).ready(function() 
{ 
    //hide the all div except first one 
    $('.msg_body:not(:first)').hide(); 

    //when the anchor is clicked content opens like shutter 
    $("a.linkclass").click(function() 
    { 
     $('.msg_body').hide("slow"); 
     $($(this).attr("href")).show("slow"); 
    });  
}); 

該網站是www.quantumrenovations.net

回答

1

你只是讓有趣的DIV,當你點擊一個鏈接,你需要抓住停泊在URI加載網頁時也(只發生一次,當頁面加載時)。

試試這個:

$(document).ready(function() { 
    //hide the all div except first one 
    $('.msg_body:not(:first)').hide(); 

    // create a reusable "generic" function 
    var showContent = function(anchor) { 
     $('.msg_body').hide("slow"); 

     $(anchor).show("slow"); 
    }; 

    // click event calls the "generic" function 
    $("a.linkclass").click(function() { 
     showContent($(this).attr("href")); 
    }); 

    // this is where you need to catch the anchor in the URI 
    var _tagIndex = window.location.href.indexOf('#'); 
    if (_tagIndex>=0) { 
     showContent(window.location.href.substr(_tagIndex)); 
    } 
}); 
+0

太謝謝你了Yanick。它完美的作品! – 2011-03-25 21:53:40

+0

很高興聽到它!不要忘記點擊「檢查」標記接受這個答案。 :) – 2011-03-25 22:05:32

相關問題