简体   繁体   English

防止在jQuery one()函数上单击跳转

[英]Prevent click jump on jQuery one() function

I'm using the one() function in jQuery to prevent multiple clicks. 我在jQuery中使用one()函数来防止多次点击。 However, when they click the element a second time, it does the annoying click jump and sends you back to the top of the page. 但是,当他们第二次单击该元素时,它会执行恼人的单击跳转并将您发送回页面顶部。

Is there a way to prevent this from happening? 有没有办法防止这种情况发生? Unbinding the click and re-binding it when the function is done has the same result (and I'm assuming that one() just unbinds the event anyways). 在完成函数时取消绑定点击并重新绑定它具有相同的结果(我假设one()只是取消绑定事件)。

A quick example of this happening: http://jsfiddle.net/iwasrobbed/FtbZa/ 这种情况的一个简单例子: http//jsfiddle.net/iwasrobbed/FtbZa/

I'm not sure if this is better or not, but you could bind a simple function that maintains the return false . 我不确定这是否更好,但你可以绑定一个维持return false的简单函数。

jQuery provides a shortcut for this with .bind('click',false) . jQuery为.bind('click',false)提供了一个快捷方式.bind('click',false)

$('.someLink').one('click', function() {
    $(this).bind('click',false);
    return false;
});

or if you have several of these links, a very efficient alternative would be to use the delegate() [docs] method to bind a handler to a common ancestor that takes care of the return false; 或者如果你有几个这样的链接,一个非常有效的替代方法是使用delegate() [docs]方法将处理程序绑定到负责return false;的公共祖先return false; for you. 为了你。

Here I just used the body , but you could use a nearer ancestor. 在这里我只是使用了body ,但你可以使用更接近的祖先。

$('.someLink').one('click', function() {
});

$('body').delegate('.someLink','click',function(){return false;});

Try changing the href so the '#' isn't being used: http://jsfiddle.net/FtbZa/1/ 尝试更改href,以便不使用'#': http//jsfiddle.net/FtbZa/1/

$('.someLink').one('click', function() {
    alert('test');
    return false;
}).attr('href', 'javascript:void(0);');

You could use the standard .click() function and a little logic: 你可以使用标准的.click()函数和一点逻辑:

1. $('.someLink').click(function(event) {
2.     event.preventDefault();
3.     if (!$(this).hasClass("clicked"))
4.         alert('This will be displayed only once.');
5.         $(this).addClass("clicked");
   });
  1. Listen to anything with the class someLink for a .click() 使用类someLink听取任何内容someLink .click()
  2. Stop the browser doing what it would normally do. 停止浏览器执行通常的操作。
  3. Check if the object has the class clicked ( Note this could be any name you wanted ) 检查对象是否已clicked该类注意这可能是您想要的任何名称
  4. It hasn't so do something. 它没有那么做。
  5. Add the class clicked so next time its clicked, it will ignore your code. 添加clicked的类,以便下次单击它时,它将忽略您的代码。

See the demo here 请在此处查看演示

The problem is that after the listener has been unbound there is nothing stopping the browser from honoring the link (Which it is treating as an anchor tag) and trying to go to it. 问题是,在侦听器未被绑定之后,没有什么能阻止浏览器尊重链接(它将其视为锚标记)并试图转向它。 (Which in this case will simply lead to the top of the page. (在这种情况下,这将简单地导致页面顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM