简体   繁体   English

浏览器后退按钮不适用于锚链接

[英]Browser back button does not work for Anchor links

In the footer of my page there a few links that point to different sections on the same page using anchor tags (# appended to the URL of the page).在我页面的页脚中,有一些链接使用锚标记(# 附加到页面的 URL)指向同一页面上的不同部分。

This works fine, just the browser back button does not work: I cannot move back to the previous page from where I had navigated to the anchored page.这工作正常,只是浏览器后退按钮不起作用:我无法从导航到锚定页面的位置移回上一页。

The simple question here is, is it possible to move back to previous page after navigating in the anchored page a few times?这里的简单问题是,在锚定页面中导航几次后是否可以返回上一页? If it is then please could you suggest how?如果是,那么请你建议如何?

Anchored page: the page that has several sections marked by the id attribute that can be pointed to by a URL with #anchorId at the end.锚定页面:具有多个由 id 属性标记的部分的页面,这些部分可以由末尾带有#anchorId的 URL #anchorId

I also faced the same problem see my question anchor links referring to the page sections not working on browser refresh, back and forward我也遇到了同样的问题,请参阅我的问题锚链接,这些链接指的是无法在浏览器刷新、后退和前进中工作的页面部分

But I had to do it the way normal links work so what I did was I manually go to that section by getting the element from the hash.但是我必须按照普通链接的工作方式来做,所以我所做的是通过从散列中获取元素来手动转到该部分。

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

This works for forward and back buttons.这适用于前进和后退按钮。 And for refresh also you need to do the same thing.对于刷新,您也需要做同样的事情。 Get the element from the hash and scroll to that element manually.从散列中获取元素并手动滚动到该元素。

History and the Back Button.历史和后退按钮。

In days of old, the back button did little more that go to the previous item in the browser's history.在过去,后退按钮的作用与浏览器历史记录中的前一项相比几乎没有。 That's changed quite a bit since then, as it keeps its own history according to a somewhat simple set of rules.从那时起,情况发生了很大变化,因为它根据一组稍微简单的规则保留了自己的历史记录。 Good luck digging through standards docs to find it though.祝你好运挖掘标准文档找到它。

UI/UX and why NOT to change expected behaviors. UI/UX 以及为什么不改变预期行为。

Please reference w3c's 'don't brek the back-button before you go making changes to a browser's default behavior.请参考w3c 的“在更改浏览器的默认行为之前不要破坏后退按钮” Its like that for a reason, following mountains of debate and defining standards.出于某种原因,经过大量的辩论和定义标准,这样做是有原因的。

Ultimately, this is what browsers do, and so this is what the users expect.归根结底,这就是浏览器所做的,这也是用户所期望的。 If you begin to subvert the behavior away from user's expectations, you're likely to start pissing off your users.如果你开始颠覆用户期望的行为,你很可能会开始激怒你的用户。 When buttons and links repeatedly don't behave as expected, users will often just give up and leave your site.当按钮和链接反复出现不符合预期的行为时,用户通常会放弃并离开您的网站。

Prevent Default.防止违约。

If you really must alter the default behavior, the using javascript would be the best way to do it:如果你真的必须改变默认行为,使用 javascript 将是最好的方法:

<a href="#id" onclick="return gotoAnchor(this);">

<script>
function gotoAnchor(elm) {
    window.event.returnValue = false;
    var url = location.href;

    location.href = elm.href;
    history.replaceState(null,null,url);

    return false;
}
</script>

http://www.the-art-of-web.com/javascript/remove-anchor-links/ http://www.the-art-of-web.com/javascript/remove-anchor-links/

Visit that site.访问那个网站。 Scroll to the bottom and use test the anchors.滚动到底部并使用测试锚点。 It's doing what you want.它正在做你想做的事。

"The following code will parse your HTML page and override the function of any links that target anchor points on the same page. The link function will be replaced with a call to the scrollIntoView method of the target element: “以下代码将解析您的 HTML 页面并覆盖以同一页面上的锚点为目标的任何链接的功能。链接功能将被替换为对目标元素的 scrollIntoView 方法的调用:

document.addEventListener("DOMContentLoaded", function() {
  var links = document.getElementsByTagName("A");
  for(var i=0; i < links.length; i++) {
    if(!links[i].hash) continue;
    if(links[i].origin + links[i].pathname != self.location.href) continue;
    (function(anchorPoint) {
      links[i].addEventListener("click", function(e) {
        anchorPoint.scrollIntoView(true);
        e.preventDefault();
      }, false);
    })(document.getElementById(links[i].hash.replace(/#/, "")));
  }
}, false);
if (document.referrer == "") {
window.open("index.php");
} else {
    window.history.go(-1);
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM