jQuery前往部分后,从网址中删除哈希 [英] Jquery remove hash from url, after going to section

查看:67
本文介绍了jQuery前往部分后,从网址中删除哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有此页面:

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末尾删除#,因此您不必弄乱滚动位置.

I have this page here:

http://raidersleafs.com/andreaparliamentlaw/professional-development.html

If you click on one of the menu items at the top, lets say 'Business Law'

it will redirect to here:

http://raidersleafs.com/andreaparliamentlaw/index.html#business

The correct section of the site, but I am looking to remove the #busness in the URL, while still going to that section. I have tried window.location.hash="" and that works, but it does not brings me to the top of the site instead of the section business law....I hope this makes sense, and is this possible?

Thanks,

解决方案

You could do:

y = window.scrollY; 
window.location.hash=""; 
window.scrollTo(0, y);

Which gets the current scroll position, changes the hash, then scrolls back to where it was before.

Or you could do it like this:

history.pushState('', document.title, window.location.pathname);

which will get rid of the # at the end of the URL as well, and then you don't have to mess with the scroll position.

这篇关于jQuery前往部分后,从网址中删除哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆