我目前使用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/
其他回答
为了防止页面跳转,你需要调用e.s stoppropagation ();在调用e.b epreventdefault()之后:
stopPropagation阻止事件沿着DOM树向上移动。更多信息请访问:https://api.jquery.com/event.stoppropagation/
在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();
});
我使用的方法是这样的:
<a href="#null" class="someclass">Text</a>
您可以使用#0作为href,因为0不允许作为id,所以页面不会跳转。
<a href="#0" class="someclass">Text</a>
对我来说最简单的就是这样做。
<a href=“#” onclick=“return false;”>一些文本</a>
使用JS的原因是大多数现代网站都依赖于它。