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

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

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


当前回答

$('a[href="#"]').click(function(e) {e.preventDefault(); });

其他回答

在#之后添加内容将页面的焦点设置为具有该ID的元素。最简单的解决方案是使用#/,即使你使用的是jQuery。但是如果你在jQuery中处理事件,event. preventdefault()是可行的方法。

你甚至可以这样写:

<a href="javascript:void(0);"></a>

我不确定这是一个更好的方法,但这是一个方法:)

解决方案#1:(普通)

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

解决方案#2:(需要javascript)

<a href="javascript:void(0);" class="someclass">Text</a>

解决方案#3:(需要jQuery)

<a href="#" class="someclass">Text</a>
<script>
$('a.someclass').click(function(e) {
    e.preventDefault();
});
</script>

您可以使用event.preventDefault()来避免这种情况。更多信息请点击:http://api.jquery.com/event.preventDefault/。

如果你想使用一个锚,你可以像其他答案一样使用http://api.jquery.com/event.preventDefault/。

您还可以使用任何其他元素,如span,并将单击事件附加到它。

$("span.clickable").click(function(){
alert('Yeah I was clicked');
});