HREF ="#"不要滚动 [英] Href="#" Don't Scroll

查看:111
本文介绍了HREF ="#"不要滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前我在我的代码中

 < a href ='#'class =closeLink>关闭< / a> 

运行一些JS来关闭一个框。我遇到的问题是,当用户点击链接时 - 当发生这种情况时,href =#将用户带到页面顶部。



如何解决这样它不会这样做?即我不能使用像onclick =return false,因为我想这将停止工作的JS?



谢谢

解决方案

通常的做法是从您的javascript click处理程序返回false。这将防止事件冒泡并取消事件的正常动作。这是我的经验,这通常是你想要的行为。



jQuery示例:

  $('。closeLink')。click(function(){
...执行close操作...
返回false;
});

如果您想简单地阻止正常操作,则可以简单地使用 preventDefault

  $('。closeLink')。click(function(e){
e.preventDefault();
...执行close操作...
});


I am pretty new to JS - so just wondering whether you know how to solve this problem.

Current I have in my code

<a href='#' class="closeLink">close</a>

Which runs some JS to close a box. The problem I have is that when the user clicks on the link - the href="#" takes the user to the top of page when this happens.

How to solve this so it doesn't do this ? i.e. I cant use someting like onclick="return false" as I imagine that will stop the JS from working ?

Thanks

解决方案

The usual way to do this is to return false from your javascript click handler. This will both prevent the event from bubbling up and cancel the normal action of the event. It's been my experience that this is typically the behavior you want.

jQuery example:

$('.closeLink').click( function() {
      ...do the close action...
      return false;
});

If you want to simply prevent the normal action you can, instead, simply use preventDefault.

$('.closeLink').click( function(e) {
     e.preventDefault();
     ... do the close action...
});

这篇关于HREF =&QUOT;#&QUOT;不要滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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