如何使用jQuery/JavaScript禁用点击? [英] How to disable click with jQuery/JavaScript?

查看:130
本文介绍了如何使用jQuery/JavaScript禁用点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要确认的链接.我正在使用javascript confirm()方法.但是,当用户单击取消"时,使链接不起作用的唯一方法是使用return false;.这是正确的方法(跨浏览器)吗?

I have a link that I would like to present with a confirmation. I am using the javascript confirm() method. But the only way for me to get the link to not work when the user clicks 'cancel' is to use return false;. Is that the correct way to do it (cross-browser)?

$('a.confirm').click(function() {
    if(confirm('Are you sure? This cannot be undone.')) {
        return true;
    }
    return false;
});

推荐答案

在事件处理程序上返回false等效于调用event.preventDefaultevent.stopPropagation,您的代码应该可以工作,但是怎么办?

Returning false on an event handler, is equivalent to call both event.preventDefault and event.stopPropagation, your code should work, but what about:

$('a.confirm').click(function() {
  return confirm('Are you sure? This cannot be undone.');
});

如果用户取消确认,它将返回false.

It will return false if the user cancels the confirm...

此处运行该代码段.

这篇关于如何使用jQuery/JavaScript禁用点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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