admin管理员组文章数量:1406308
I'm using the one() function in jQuery to prevent multiple clicks. 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).
A quick example of this happening: /
I'm using the one() function in jQuery to prevent multiple clicks. 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).
A quick example of this happening: http://jsfiddle/iwasrobbed/FtbZa/
Share Improve this question asked Mar 2, 2011 at 22:54 iwasrobbediwasrobbed 46.7k21 gold badges152 silver badges195 bronze badges4 Answers
Reset to default 4I'm not sure if this is better or not, but you could bind a simple function that maintains the return false
.
jQuery provides a shortcut for this with .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 mon ancestor that takes care of the return false;
for you.
Here I just used the body
, but you could use a nearer ancestor.
$('.someLink').one('click', function() {
});
$('body').delegate('.someLink','click',function(){return false;});
Try changing the href so the '#' isn't being used: http://jsfiddle/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:
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");
});
- Listen to anything with the class
someLink
for a.click()
- Stop the browser doing what it would normally do.
- Check if the object has the class
clicked
(Note this could be any name you wanted) - It hasn't so do something.
- Add the class
clicked
so next time its clicked, it will ignore your code.
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.
本文标签: javascriptPrevent click jump on jQuery one() functionStack Overflow
版权声明:本文标题:javascript - Prevent click jump on jQuery one() function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745006057a2637269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论