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

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

问题描述

我有一个链接,当点击该链接时,会对某些 div 等执行一些动画功能,并且效果很好.我的问题是当我多次单击按钮等时,动画看起来很奇怪.我希望这样,当我的按钮被点击时,该按钮被禁用"了 2 秒.

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.

我的链接是这样的:

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

推荐答案

是的,你绝对可以做到这一点.

Yes, you can absolutely do this.

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

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; 来自您的点击处理程序将具有与 preventDefault 相同的效果,但添加了防止事件冒泡 DOM 的操作.

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天全站免登陆