2011-03-25 66 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

相关问题