“禁用”临时点击链接? [英] "Disable" a link temporarily when clicked?

查看:184
本文介绍了“禁用”临时点击链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接,当点击,执行一些动画功能到一些div等,并工作非常好。我的问题是当我点击按钮多次等,动画看起来很奇怪。我想这样,当我的按钮被点击,按钮是禁用说2秒。



我不认为我需要发布代码



我的链接是这样:

 < a href =button>点击< / a> 


解决方案

是的,你绝对可以这样做。 >

添加一个点击处理程序,并在动画运行时使用 preventDefault(),直到完成。有关 preventDefault 的详情,请参阅jQuery文档。您可以使用标志或在第一次单击后更改您的点击处理程序来完成此操作。这是一个有标志的例子。

  var isAnimating = false; 
$(#animationLink)。click(function(e){
if(!isAnimating){
isAnimating = true;
setTimeout(isAnimating = false,2000 ); //在2秒内设置为false
//或者你可以等到动画完成
//调用动画代码
} else {
e.preventDefault() ;
}
});

return false; 具有与preventDefault相同的效果,增加了防止DOM冒泡的事件。


I have a link that when clicked, performs some animation functions to some divs etc and works very well. My problem is when I click the button more than once etc, the animation looks weird. I would like it so, that when my button is clicked, the button is "disabled" for say 2 seconds.

I don't think I need to post the code, but just ask if you think you need it.

My link is like so:

<a href="button">Click</a>

解决方案

Yes, you can absolutely do this.

You add a click handler and use preventDefault() when the animation is running until it is complete. More about preventDefault is in the jQuery docs. You can use a flag or change your click handler after the first click to accomplish this. Here's an example with a flag.

var isAnimating = false;
$("#animationLink").click(function(e) {
    if(!isAnimating) {
        isAnimating = true;
        setTimeout("isAnimating = false", 2000); // set to false in 2 seconds
        // alternatively you could wait until your animation is done
        // call animation code
    } else {
        e.preventDefault();
    }
});

return false; from your click handler will have the same effect as preventDefault with the added action of preventing event bubbling up the DOM.

这篇关于“禁用”临时点击链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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