Only show specific div with anchor

让人想犯罪 __ 提交于 2019-12-25 07:24:17

问题


First of all, my coding skills are very basic so please bear with me.

I am making a website for an art exhibition which has many people contributing.

I have a page called profiles.html which contains all of the profile of the people who have contributed.

Each profile is contented within a DIV called profiles along with an anchor tag of the persons name.

If people go to the website www.example.com/profiles.html they will see all of the profiles.

However when they go to
www.example.com/profiles.html#example.name they will only see the profile of that person.

I have looked around the internet to try and find answers but I have not found any.

UPDATE:

A follow up question. Is it possible to show/ load extra content in the profiles.html#examplename that would not be seen in profiles.html


回答1:


Assuming you have:

  • A wrapper with class="profile" around each profile
  • And id on each pofile wrapper, matching your hash (like "example.name")

Then I believe you want something like this:

// 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();
}


来源:https://stackoverflow.com/questions/14988930/only-show-specific-div-with-anchor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!