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