How do I navigate to hashtag after page load?

后端 未结 5 1093
忘掉有多难
忘掉有多难 2021-02-01 07:38

I want to do the inverse of what I\'ve been finding so far. I\'m setting a lot of heights with js and I want to navigate to the hashtag in the url after the page has loaded. I\'

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-01 08:41

    You changed your question.

    Check out this solution. https://stackoverflow.com/a/2162174/973860 so you understand what is going on and how to implement a cross browser solution.

    NOTICE: At the bottom he mentions a jquery plugin that will do what you need.

    http://benalman.com/projects/jquery-hashchange-plugin/

    This plugin will allow you to do something like this. This will work for your current page. But you may want to modify it to be more robust.

    $(function(){
    
        // Bind the event.
        $(window).hashchange( function(){
            // get the hash
            var hash = window.location.hash;
            // click for your animation
            $('a[href=' + hash + ']').click();
        })
    
        // Trigger the event (useful on page load).
        $(window).hashchange();
    
    });
    

提交回复
热议问题