目前,我的浏览器后退按钮是跳转的内部链接在我的网站。每个部分都有一个ID,导航跳到具有相应ID的部分。我希望我的网站返回,如果用户来自Google,而不是在我的页面上跳来跳去。
我该怎么做呢?有没有办法控制浏览器后退按钮在我的网站上做什么?
发布于 2016-11-20 20:53:03
这个答案已经修改,以显示正确的信息。通过改变A元素的功能方式,它不会将#添加到URL中,而且当他们单击后退按钮时,他们将被带到他们访问您的站点之前所在的页面(只要您的站点是一个页面)。
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.js"></script>
</head>
<!--These are your links that will take the user to each part of the page.
Change the value in the onclick function to the name of your div-->
<a href="Javascript:void(0);" onclick="goToByScroll('div1')">Click here to go to Div 1</a>
<a href="Javascript:void(0);" onclick="goToByScroll('div2')">Click here to go to Div 2</a>
<!--Add ID attributes to your divs, this is what you will put in the goToByScroll Onclick value.-->
<div id="div1" style="margin-top: 700px">
<!--Div 1 Info-->
Div 1
</div>
<div id="div2" style="margin-top: 700px">
<!--Div 2 info-->
Div 2
</div>
<!--This is the JS function. I've added in a smooth scrolling technique to make it not choppy.-->
<script type="text/javascript">
function goToByScroll(id){
$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
</script>
https://stackoverflow.com/questions/40709112
复制相似问题