2013-06-18 33 views
0

所有事件處理程序有沒有辦法做到。如何暫時禁用連接到控制

$("#controlId").suspendEvents(); 

$("#controlId").resumeEvents(); 

我所知道的preventDefaultstopPropagation。我想從事件之外做。 請在回答中考慮以下幾點。

  • 我不能修改這些綁定的事件。
  • 我不知道綁定事件(雖然它可能會帶我長的時間來做到這一點)。所以它不可能.off(),然後逐一添加它們。
+0

有支持的,雖然你可以通過$迭代._數據的公共方法(ELEM,「事件」),對象包含使用jQuery綁定的所有事件。爲了檢查使用原生JavaScript綁定的事件,如果使用內聯綁定事件,可以檢查相應的屬性,我不知道這是否適用於任何其他JavaScript綁定處理程序的方法,未經測試。 –

+0

@roasted謝謝。我會仔細看看的。 – Gnani

回答

0

我能夠從其他兩個問題放在一起的答案。

1. Bind an event handler to front of the queue

2. Attach handler to all events in a control

的想法是事件處理程序與e.stopImmediatePropagation綁定到隊列的所有事件的前面。如果這能夠得到改善,我會很高興。

的解決方案......

$.fn.preBind = function (type, data, fn) { 
    this.each(function() { 
     var $this = $(this); 

     $this.bind(type, data, fn); 

     $.each(type.split(/ +/), function() { 
      var currentBindings = $this.data('events')[this]; 
      if ($.isArray(currentBindings)) { 
       currentBindings.unshift(currentBindings.pop()); 
      } 
     }); 
    }); 
    return this; 
}; 

$.fn.suspendEvents = function() { 
    this.preBind("click keydown keyup keypress mouseover mouseenter mouseout mouseleave mousedown mouseup mousemove change blur focus focusin focusout scroll resize load unload beforeunload", null, blockEvents); 
} 

$.fn.resumeEvents = function() { 
    var _this = this; 
    $.each("click keydown keyup keypress mouseover mouseenter mouseout mouseleave mousedown mouseup mousemove change blur focus focusin focusout scroll resize load unload beforeunload".split(/ +/), function() { 
     _this.unbind(this, blockEvents); 
    }); 
} 

function blockEvents(e) { 
    e.stopImmediatePropagation(); 
} 

現在我可以使用

$("#controlId").suspendEvents(); 
$("#controlId").resumeEvents(); 

編輯:修改resumeEvents()克服IE的問題。

0

一切都泡了,所以身體捕捉任何事件,並防止他們。

替代

var myCtlrs = $("all i want").attr("disabled", disabled"); 

然後

myCtlrs.removeAttr("disabled"); 
+1

當它冒起它已經執行的權利?或者我錯過了什麼。 – Gnani

+0

好吧,關於我發佈的替代方案是什麼? – Luke

+0

@Gnani你是對的 –