从JQuery中的元素获取点击处理程序 [英] Get click handler from element in JQuery

查看:98
本文介绍了从JQuery中的元素获取点击处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取对JQuery中某个元素的点击处理程序的引用?

How can I get a reference to the click handler of an element in JQuery?

这就是我想要做的:

存储点击处理程序, 将点击处理程序更改为下一次点击, 恢复原始的点击处理程序

Store the click handler, Change the click handler for the next click, Restore the original click handler

var originalClick = $(settings.currentTarget).click;
$(settings.currentTarget).off("click");

$(settings.currentTarget).click(function (e) {
    e.preventDefault();
    settings.model.modal.close();

    $(settings.currentTarget).off("click");
    $(settings.currentTarget).click(originalClick);
});

上面的代码第一次起作用,但是,当我再次单击该元素时,它会失败:

The above code works the first time, however, when I click on the element again it fails:

 Uncaught TypeError: Object [object HTMLAnchorElement] has no method 'on' 

更新:
我现在意识到这是我要尝试的非常糟糕的设计.我已经通过在视图模型中保留可见性布尔值来解决了此问题,如果属实,请不要重新打开模式.

Update:
I now realize that this is a really bad design that I am trying to do. I have resolved this issue by maintaining a visibility boolean in my viewmodel, if it is true, don't re-open the modal.

推荐答案

$(...).click是jQuery click()方法,而不是事件处理程序.

$(...).click is the jQuery click() method, not your event handler.

您可以使用未记录的内部API 来获取事件处理程序列表:

You can use an undocumented internal API to get the list of event handlers:

jQuery._data( elem, "events" );

这篇关于从JQuery中的元素获取点击处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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