jQuery 取消绑定,然后再次绑定

发布于 2024-10-08 06:54:23 字数 243 浏览 0 评论 0原文

我有我的功能(我需要稍后取消绑定):

$("#closebtn").click(function(){
    $.address.value('/x');
});

我在事件后取消绑定它:

$("#closebtn").unbind("click");

现在我想将我的“单击”功能再次关联到我的#closebtn,我该怎么办? 谢谢 d

i've my function (i need to unbind later):

$("#closebtn").click(function(){
    $.address.value('/x');
});

I unbind it after an event:

$("#closebtn").unbind("click");

Now I want to associate my "click" function again to my #closebtn, how can i do ?
thank you
d

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将您的函数分配给变量

var closebtn = function() {
    $.address.value('/x');
};

然后您可以与该变量绑定

$("#closebtn").click(closebtn);

Assign your function to a variable

var closebtn = function() {
    $.address.value('/x');
};

Then you can bind with the variable

$("#closebtn").click(closebtn);
温柔少女心 2024-10-15 06:54:23

如果它实际上是一个按钮,您可以禁用它,而不是取消绑定/重新绑定。或者,添加一个 if 语句,以便该函数在您不希望它“触发”时返回 false - 我认为这比绑定/取消绑定/重新绑定更有效,后者相当昂贵。

也许是这样的?

$("#closebtn").click(
    function() {
        if (inEvent) {
            return false
        }
        $.address.value('/x')
    })

$('someOtherElement').click(
    function() {
        inEvent = 1
        // ...
        // do the processing where you don't
        // want the function to fire
        .. ...
        inEvent = 0
    })

If it is actually a button, could you just disable it, rather than unbind/re-bind. Or, add an if statement so the function returns false when you don't want it to 'fire' - I think that would be more efficient than bind/un-bind/rebind, which is fairly expensive.

Something like this, maybe?

$("#closebtn").click(
    function() {
        if (inEvent) {
            return false
        }
        $.address.value('/x')
    })

$('someOtherElement').click(
    function() {
        inEvent = 1
        // ...
        // do the processing where you don't
        // want the function to fire
        .. ...
        inEvent = 0
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文