JQuery: Disable click event

前端 未结 5 1990
梦如初夏
梦如初夏 2021-01-15 09:56

I want to disable all click events on my page but some of events are still getting called, suppose I have html as below

5条回答
  •  伪装坚强ぢ
    2021-01-15 10:44

    try this plugin to temporary disable onclick AND jQuery click events:-

    $.fn.disableClick = function (disable){
        this.each(function() {
            if(disable){
                if(this.onclick)
                    $(this).data('onclick', this.onclick).removeAttr('onclick');
                if($._data(this, 'events') && $._data(this, 'events').click)
                    $(this).data('click', $.extend(true, {}, $._data(this, 'events').click)).off('click');
            }
            else{
                if($(this).data('onclick'))
                    this.onclick = $(this).data('onclick');
                if($(this).data('click'))
                    for(var i in $(this).data('click'))
                        $(this).on('click', $(this).data('click')[i].handler);
            }
        });
        return this;
    };
    
    
    //disable clicks
    $('#parent *').disableClick(true);
    
    //enable clicks
    $('#parent *').disableClick(false);
    

    DEMO

提交回复
热议问题