空URL哈希会导致页面跳转到js事件 [英] Empty URL hash causes page to jump on js events

查看:175
本文介绍了空URL哈希会导致页面跳转到js事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张带有下一个和上一个按钮的照片库。如果其中一个JavaScript方法由于某种原因被某个或某个按钮单击时出现故障,则会为该网址添加散列,即www.google.com#。我知道散列可以被赋予一个div id跳转到页面的那一部分,但是当它是空白的时候,它会跳过我的页面几次,我不确定它的目标是什么。我想过试图从网址中删除散列,但是我必须确保在每一个动作上,这似乎是不好的做法。如果散列对页面上的操作没有影响,我宁愿选择。

解决方案

不要使用 href =#不正确。

改为使用 href =javascript:;或其变体(我个人使用 javascript:void(null);

明确指出链接不会转到另一个位置,而是由JavaScript处理。

b $ b

I have a gallery of photos with a next and previous button. If one of my javascript methods is broken for one reason or another when one of the buttons is clicked it will add a hash to the url i.e. www.google.com# . I know the hash can be given a div id to jump to that part of the page but when it's blank it jumps around my page a few times and I'm not sure what it's targeting. I thought of attempting to remove the hash from the url but then I'd have to ensure that on every action and that seems like bad practice. I would prefer if the hash just made no difference to the actions on the page.

解决方案

Don't use href="#", it is improper.

Instead, use href="javascript:;" or a variant thereof (personally I use javascript:void(null);)
This explicitly states that the link does not go to another location, but is handled by JavaScript.

这篇关于空URL哈希会导致页面跳转到js事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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