我目前使用jQuery的<a>标签来启动像点击事件等事情。
例如<a href="#" class="someclass">Text</a>
但我讨厌“#”使页面跳转到页面顶部。我能做什么呢?
我目前使用jQuery的<a>标签来启动像点击事件等事情。
例如<a href="#" class="someclass">Text</a>
但我讨厌“#”使页面跳转到页面顶部。我能做什么呢?
当前回答
为了防止页面跳转,你需要调用e.s stoppropagation ();在调用e.b epreventdefault()之后:
stopPropagation阻止事件沿着DOM树向上移动。更多信息请访问:https://api.jquery.com/event.stoppropagation/
其他回答
如果你想要迁移到同一页面上的锚节,而不需要页面跳转,请使用:
只要用“#/”而不是“#” 如
<a href="#/home">Home</a>
<a href="#/about">About</a>
<a href="#/contact">contact</a> page will not jump up on click..
您可以使用event.preventDefault()来避免这种情况。更多信息请点击:http://api.jquery.com/event.preventDefault/。
在jQuery中,当你处理点击事件时,返回false以阻止链接响应通常的方式阻止默认操作,即访问href属性,从发生(根据PoweRoy的评论和Erik的回答):
$('a.someclass').click(function(e)
{
// Special stuff to do when this link is clicked...
// Cancel the default action
e.preventDefault();
});
解决方案#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>
<a href="#! "">链接</a>和<a href="#/">链接</a>不工作,如果一个人必须点击相同的输入不止一次。它只需要在多个输入上进行1次事件单击。
最好的方法仍然是使用<a href="#">Link</a>
然后,
event.preventDefault();