【问题标题】:Only show specific div with anchor仅显示带有锚点的特定 div
【发布时间】:2013-02-20 20:06:59
【问题描述】:

首先,我的编码技能非常基础,请多多包涵。

我正在为一个有很多人参与的艺术展览制作一个网站。

我有一个名为profiles.html 的页面,其中包含所有贡献者的个人资料。

每个配置文件都包含在一个称为配置文件的 DIV 中,并带有一个人名的锚标记。

如果人们访问该网站 www.example.com/profiles.html 他们将看到所有的配置文件。

但是当他们去
www.example.com/profiles.html#example.name 他们只会看到那个人的个人资料。

我已经在互联网上四处寻找答案,但我没有找到任何答案。

更新:

一个后续问题。是否可以在profiles.html#examplename中显示/加载在profiles.html中看不到的额外内容

【问题讨论】:

    标签: jquery html tags anchor


    【解决方案1】:

    假设你有:

    • 每个配置文件都带有class="profile" 的包装器
    • 每个 pofile 包装器上的 id 与您的哈希匹配(如“example.name”)

    那么我相信你想要这样的东西:

    // On page load
    $(document).ready(function(){
    
        // React to clicks on anchors
        window.onhashchange = hashChanged;
    
        // React to directly type urls
        hashChanged();
    
    });
    
    // When the URL hash (#something) changes
    function hashChanged() {
        // hide all profiles
        $('.profile').hide();
        // get the current hasj minus '#'
        var profileId = window.location.hash.substr(1);
        // show only the appropriate profile
        $('#' + profileId).show();
    }
    

    【讨论】:

    • "example.name",因为 "." 而不起作用,删除它,它就可以正常工作了。感谢您提供快速优质的帮助。
    • 一个后续问题。是否可以在profiles.html#examplename中显示/加载在profiles.html中看不到的额外内容
    • 当然,您可以.show() 隐藏CSS 中的元素,或者使用ajax 来.load() 更多内容。您必须从 hashChanged 函数中执行此操作。
    • 你能告诉我这是如何在 JSfiddle 中工作的吗?我是一个视觉学习者。另外,这样可以为不同的人显示不同的内容吗?
    • 如果你设置了一个 jsfiddle,我可以看看并调整它。
    猜你喜欢
    • 2013-08-07
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2013-11-29
    相关资源
    最近更新 更多