簡體   English   中英

指向頁面部分的錨鏈接不適用於瀏覽器刷新、后退和前進

[英]anchor links referring to the page sections not working on browser refresh, back and forward

我制作了一個帶有頁面內鏈接的 html 頁面。

<div>
<a href="#first">first</a>
<a href="#second">second</a>
<div id="first">First div</div>
<div id="second">Second div</div>
</div>

但是此代碼不適用於頁面刷新和瀏覽器后退和前進按鈕。 有誰知道如何解決這個問題。 我不是在尋找 html5 的歷史 API 和 jquery 的歷史插件。 想要以基本格式創建它。

好的,經過多次嘗試,我已經找到了方法。 現在這滿足了我的目的,所以我繼續使用這個解決方案。

$(window).on('hashchange', function () 
{
    var top = $(window.location.hash).offset().top;
    $(window).scrollTop(top);
});

這適用於前進和后退按鈕。 為了刷新我們也需要做同樣的事情。 從散列中獲取元素並手動滾動到該元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM