When hash(#) link pressed in some websites, does not add hash to url?

为君一笑 提交于 2019-12-13 03:38:54

问题


I have hash(#) links in my own website, and I realised that when I click on them, a hash sign will always appear at the end of my url. But in other web pages that I was fiddling around with, no matter how many times I clicked the link which had a source of '#', it would never add a hash to the end of my URL. Why is this?


回答1:


They probably have javascript attached to the link that prevents the link action from starting.

A simple

return false;

will do this




回答2:


It's normal behaviour of anchor links. If you click link with href like something#anchor, you are redirected to url something (if you aren't already on it) and #anchor appended to url. This anchor refers to some part of page. Single "#" sign is simply the empty anchor.

To avoid this sign appearing in url, you should remove href attribute entirely (but sometimes after this action you need to fix stylesheet because appearance of links depends on href attribute presence). Or, if you are using javascript handlers of click event, you should change them to return false.



来源:https://stackoverflow.com/questions/10264184/when-hash-link-pressed-in-some-websites-does-not-add-hash-to-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!