我在这里有这个页面:
http://raidersleafs.com/andreaparliamentlaw/professional-development.html
如果您单击顶部的其中一个菜单项,请说“商业法”
它会重定向到这里:
http://raidersleafs.com/andreaparliamentlaw/index.html#business
网站的正确部分,但我希望删除网址中的#busness
,同时仍然会转到该部分。我已经尝试了window.location.hash=""
并且有效,但它没有把我带到网站的顶部而不是商业法部分....我希望这是有道理的,这可能吗?
谢谢,
答案 0 :(得分:3)
你可以这样做:
y = window.scrollY;
window.location.hash="";
window.scrollTo(0, y);
获取当前滚动位置,更改散列,然后滚动回到之前的位置。
或者你可以这样做:
history.pushState('', document.title, window.location.pathname);
它也会删除URL末尾的#
,然后你不必弄乱滚动位置。