[英]How to disable all ng-click and ng-submit event
有什么辦法,我如何全局(在使用中)禁用和啟用所有ng-click和ng-submit事件?
例如,當用戶離線時,我想禁用所有操作,直到他恢復連接。
我試圖將所有元素與onClick事件綁定,該事件將調用stopImmediatePropagation,但是沒有用。
$('*[ng-click]').click(function( event ) {
event.stopImmediatePropagation();
});
這個問題也與此稍有不同: 對所有類型的元素禁用ng-click某些應用條件
我想從服務中全局禁用/啟用APP中的所有事件,但無法修改APP中所有元素上的所有ng- *調用。
也嘗試包含return false
:
$('*[ng-click]').click(function( event ) {
event.stopImmediatePropagation();
return false;
});
片段
下面的代碼片段演示了附加到單個<a>
多個事件處理程序也可以工作。
$(function () { $("a").click(function () { alert("Hello!"); return false; }); $("a").click(function () { alert("Bye!"); return false; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <a href="#">Click Me</a>
因此,最后我最終使用jquery暫時禁用了頁面上的所有事件 。.我從此插件http://ignitersworld.com/lab/eventPause.html受到啟發,由於某種原因,該插件不起作用(沒有任何錯誤)
因此,我學習了主要部分並將其放在使用jquery v2.1.1現在可以正常工作的此類中:
var EventManager = function() {
var self = this;
var nullFun=function(){};
var getIndex = function(array,value){
for(var i=0; i< array.length; i++){
if(array[i]==value){
return i;
}
}
return -1;
};
this.pauseEvent = function(elm,eventAry){
var events = $._data(elm, "events");
if (events) {
$.each(events, function(type, definition) {
if((getIndex(eventAry,type)!=-1)||(eventAry=='')){
$.each(definition, function(index, event) {
if (event.handler.toString() != nullFun.toString()){
if(!$._iwEventPause) $._iwEventPause = {};
$._iwEventPause["iw-event" + event.guid] = event.handler;
event.handler = nullFun;
}
})
}
})
}
};
this.activeEvent = function(elm,eventAry){
var events = $._data(elm, "events");
if (events) {
$.each(events, function(type, definition) {
if((getIndex(eventAry,type)!=-1)||(eventAry=='')){
$.each(definition, function(index, event) {
if (event.handler.toString() == nullFun.toString()){
event.handler = $._iwEventPause["iw-event" + event.guid];
}
})
}
})
}
};
this.disableAll = function(el) {
el = el || $('*');
el.each(function() {
self.pauseEvent($(this)[0], '');
});
self.pauseEvent($(window)[0], '');
};
this.enableAll = function(el) {
el = el || $('*');
el.each(function() {
self.activeEvent($(this)[0], '');
});
self.activeEvent($(window)[0], '');
};
return this;
};
var eManager = new EventManager();
eManager.disableAll();
eManager.enableAll();
這將遍歷窗口對象和頁面上的所有元素,將其事件處理程序移至_iwEventPause對象,並用虛擬函數替換處理程序。啟用后,它將向后移動處理程序,以便通常調用它們。
此解決方案不處理禁用后添加的事件處理程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.