我有一个菜单栏。菜单栏上的每个项目都是一个超链接:
<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/