我目前使用jQuery的<a>标签来启动像点击事件等事情。

例如<a href="#" class="someclass">Text</a>

但我讨厌“#”使页面跳转到页面顶部。我能做什么呢?


当前回答

您可以使用#0作为href,因为0不允许作为id,所以页面不会跳转。

<a href="#0" class="someclass">Text</a>

其他回答

如果你想要迁移到同一页面上的锚节,而不需要页面跳转,请使用:

只要用“#/”而不是“#” 如

<a href="#/home">Home</a>
<a href="#/about">About</a>
<a href="#/contact">contact</a> page will not jump up on click..

我用过:

<a href="javascript://nop/" class="someClass">Text</a>
$('a[href="#"]').click(function(e) {e.preventDefault(); });

为了防止页面跳转,你需要调用e.s stoppropagation ();在调用e.b epreventdefault()之后:

stopPropagation阻止事件沿着DOM树向上移动。更多信息请访问:https://api.jquery.com/event.stoppropagation/

只使用

<a href="javascript:;" class="someclass">Text</a>

JQUERY

$('.someclass').click(function(e) { alert("action here"); }