开发者

Accessing a dynamic content from another page

开发者 https://www.devze.com 2023-02-20 01:37 出处:网络
I\'m working on a websites and the clients wants to be able to access the dynamic content on the services page from every other page on the website but the code below makes it to display the first \"d

I'm working on a websites and the clients wants to be able to access the dynamic content on the services page from every other page on the website but the code below makes it to display the first "div" in the list instead of the one with the anchor that was clicked on.

$(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("h开发者_JAVA技巧ref")).show("slow");
    });    
});

The site is www.quantumrenovations.net


You only show the interesting DIV when you click on a link, you need to catch the anchor in the URI when the page loads also (happens only once, when the page load).

Try this :

$(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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号