jquery - 选择菜单项后如何避免浏览器地址栏中出现#

标签 jquery html

我有一个菜单栏。菜单栏上的每个项目都是一个超链接:

  <a href="#" title="item1">

根据菜单选择,我显示了一些 div 并隐藏了其余的 div。一切正常。如果我点击菜单项,结果显示在同一页面上,这很好,但在浏览器地址栏上,# 符号显示为 localhost/abc#,这导致激活浏览器后退 ( <-- ) 按钮,这可能会给用户一个错误的假设,即他们可以单击浏览器上的后退按钮。

有没有办法避免在地址栏中显示#

我在事件处理函数中使用 return false; 语句来禁用 href 链接。

最佳答案

有没有办法避免在地址栏中显示#?

是的,您可以通过更改

来避免 #
<a href="#" title="item1">

<a href="javascript:void(0)" title="item1">

为什么显示#?(来自MDN)

href - This is the single required attribute for anchors defining a hypertext source link. It indicates the link target, either a URL or a URL fragment. A URL fragment is a name preceded by a hash mark (#), which specifies an internal target location (an ID) within the current document. URLs are not restricted to Web (HTTP)-based documents. URLs might use any protocol supported by the browser. For example, file, ftp, and mailto work in most user agents.

关于jquery - 选择菜单项后如何避免浏览器地址栏中出现#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13077160/

相关文章:

javascript - 使用jquery按名称获取多个隐藏值

javascript - 为什么我的选择列表不显示?

javascript - 当鼠标悬停在 IFRAME 上时防止在父页面上滚动

javascript - 即时更改渐变的颜色

html - Bootstrap 输入边界重叠

javascript - 无法将标题标签居中放置在图像上

jquery - 使用 "enter"键发送带有自定义复选框/ radio 图像的表单

javascript - TinyMCE 和 HTML

javascript - 从使用 jqplot 的网站上的绘图中获取原始数据

html - 使用没有宽度的css float