如何打破 event.stopImmediatePropagation [英] how to break event.stopImmediatePropagation

查看:20
本文介绍了如何打破 event.stopImmediatePropagation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的代码.

<td>
 <span class='ui-button-next'>NEXT</span>
</td>

此应用程序包含一个库,不允许编辑.单击ui-button-next"时,它们会调用 event.stopImmediatePropagation() 来停止某些操作.

this application contains a library and it is not allowed to edit. On clicking "ui-button-next", they are calling event.stopImmediatePropagation() to stop something.

我想在用户点击这个跨度"而不触摸那个库时调用一个函数.

I want to call a function when user clicking on this "span" without touching that Library.

我的自定义代码如

$(".ui-button-next").click(function(){
});

$(".ui-button-next").bind("click",function(){
});

$(".ui-button-next").on("click",function(){
});

由于库上的 event.stopImmediatePropagation() 而无法工作.任何解决方法?

are not working due to event.stopImmediatePropagation() on the library. any workaround?

非常感谢!

推荐答案

您可以访问底层事件列表(数组)并在第一个插入您的新点击处理程序,它会正常触发,但请注意执行顺序在您的情况下应该不是问题:

You can access the underlying events list (array) and insert your new click handler at the first, it will be triggered normally, however note that the order of execution should not be a problem in your case:

//this is the inner handler
$('.ui-button-next').click(function(e){
   e.stopImmediatePropagation();     
});
//this is the handler of your own
$('.ui-button-next').click(function(e){       
    alert('OK');
});

//get click handlers list
var clicks = $._data($('.ui-button-next')[0], 'events')['click'];
//get the last added handler (which is your own handler) and put it at the beginning
clicks.unshift(clicks.pop());

演示.

UPDATE:为了保持执行顺序(首先执行默认处理程序,然后执行所有额外添加的处理程序),我认为您必须通过删除所有e.stopImmediatePropagation() 方法,我们这里必须使用 eval() 方法.请注意,在这种情况下使用该方法是完全可以的.

Demo.

UPDATE: To keep the order of execution (the default handlers are executed first, all the additional added handlers are executed after), I think you have to modify the default handlers by removing all the e.stopImmediatePropagation() methods, we have to use the eval() method here. Note that using that method in this case is totally OK.

//get click handlers list
var clicks = $._data($('.ui-button-next')[0], 'events')['click'];
//remove the e.stopImmediatePropagation() in each handler
$.each(clicks, function(i,e){
  var handlerText = e.handler.toString()
                     .replace(/e.stopImmediatePropagation\(\)/g,'');    
  eval("e.handler = " + handlerText);
});

这篇关于如何打破 event.stopImmediatePropagation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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