jQuery rebind()在特定时间禁用事件,然后重新绑定它们 [英] jquery rebind() disable events for a specific time, then rebind them

查看:116
本文介绍了jQuery rebind()在特定时间禁用事件,然后重新绑定它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮后,我的脚本将加载新内容.只要加载中,我都不希望单击元素列表.

After click on a button my script loads new content. As long as it is loading I don't want a list of elements to be clickable.

是否有任何方法可以取消绑定那些元素的点击事件,加载内容,然后正确地重新绑定"事件?

IS there any way to unbind the click-event of those elements, load the content and then "rebind" the events correctly?

我要防止的是每个点击功能/元素中的if语句.

What I want to prevent is a if-statement inside every click-function/element.

这是我尝试过的:

$(load_button).click(function(){
    $('a').each(function(){
        var dis_e = $(this).data('events');
        $(this).data('disabled-events', dis_e);
    }).unbind('click');

    $(some_content).load(function(){
        $('a').each(function(){
            var dis_e = $(this).data('disabled-events');
            $(this).removeData('disabled-events');
            $(this).bind('click', dis_e);
        });
    });
});

更新:

它还应该防止元素(在这种情况下)提醒点击":

it should also prevent the elements from (in this case) alerting 'click':

$('a').click(function(){
   alert('click');
});

推荐答案

根据您的回答,我编写了以下插件:

Based on your answers i wrote the following plugin:

(function($){
    $.fn.rebind = function(){
        var init = 'init-rebind';
        return this.each(function() {
            var is_init = $(this).data(init);
            if (!is_init){
                $(this).data(init, true);
                $(this).bind('click.rebind mouseenter.rebind mouseleave.rebind', function(e){
                    if ($(this).hasClass('disabled')){
                        e.stopImmediatePropagation();
                        e.preventDefault();
                    }
                });
            }
        });
    };
})(jQuery);

我决定不添加全局变量,而是添加/删除"disabled"类,以便也可以在CSS中进行控制.

Instead of using a global variable i decided to add/remove the class 'disabled' in order to also have control within css.

查看演示

THX to all!

THX to all!

更新:检查出来了!

这篇关于jQuery rebind()在特定时间禁用事件,然后重新绑定它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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