如何防止点击“#”链接跳转到页面顶部? [英] How to prevent a click on a '#' link from jumping to top of page?

查看:69
本文介绍了如何防止点击“#”链接跳转到页面顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

但我讨厌'#'让页面跳到页面顶部。我可以做些什么呢?

在jQuery中,处理单击事件时,返回false以停止链接通过响应通常的方式阻止默认操作,即访问 href 属性(根据PoweRoy的注释和 {
//点击此链接时要做的特殊操作...

/ $('a.someclass')。 /取消默认操作
e.preventDefault();
});


I'm currently using <a> tags with jQuery to initiate things like click events, etc.

Example is <a href="#" class="someclass">Text</a>

But I hate how the '#' makes the page jump to the top of the page. What can I do instead?

解决方案

In jQuery, when you handle the click event, return false to stop the link from responding the usual way prevent the default action, which is to visit the href attribute, from taking place (per PoweRoy's comment and Erik's answer):

$('a.someclass').click(function(e)
{
    // Special stuff to do when this link is clicked...

    // Cancel the default action
    e.preventDefault();
});

这篇关于如何防止点击“#”链接跳转到页面顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆