【问题标题】:Accessing a dynamic content from another page从另一个页面访问动态内容
【发布时间】:2011-07-23 05:25:33
【问题描述】:

我正在开发一个网站,客户希望能够从网站上的所有其他页面访问服务页面上的动态内容,但下面的代码使其显示列表中的第一个“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

【问题讨论】:

    标签: jquery jquery-plugins jquery-selectors


    【解决方案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));
       }
    });
    

    【讨论】:

      猜你喜欢
      • 2012-01-13
      • 1970-01-01
      • 2014-06-02
      • 2022-01-09
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 2020-12-21
      • 2018-12-16
      相关资源
      最近更新 更多