[英]Trigger Custom jQuery Event and delay it
I want to trigger the event of an element being removed from the DOM. 我想触发从DOM中删除元素的事件。 Sadly I must use the original .remove()
, because it is being called from an external script. 可悲的是,我必须使用原始的.remove()
,因为它是从外部脚本中调用的。 So I extended jQuery in order to catch the event: 因此,我扩展了jQuery以捕获事件:
var oldRemove = $.fn.remove;
$.fn.remove = function(){
this.trigger('remove');
return oldRemove.apply(this, arguments);
};
This works fine. 这很好。 I can use it like this: 我可以这样使用它:
$('ul').find('li').on('remove', function(e){
console.log('EVENT REGGED');
});
I want to use this code to highlight the deletion of elements . 我想使用此代码突出显示元素的删除 。 An example: Set the color of the element to red, wait 2 seconds and then delete it. 示例:将元素的颜色设置为红色,等待2秒钟,然后将其删除。
What is the most elegant way to do this? 最优雅的方法是什么? I already experimented with e.preventDefault()
and e.stopPropagation
or to return something from the trigger, but nothing worked. 我已经尝试过使用e.preventDefault()
和e.stopPropagation
或从触发器返回某些内容,但是没有任何效果。
Here's a fiddle so you can test it: http://jsfiddle.net/xahthnjz/ 这是一个小提琴,您可以对其进行测试: http : //jsfiddle.net/xahthnjz/
What can I do in order to first highlight the element and then delay the deletion? 为了突出显示元素然后延迟删除,我该怎么办?
You can use a setTimeout within your custom jQuery function to handle the removal of the item after applying the color change. 您可以在自定义jQuery函数中使用setTimeout来应用颜色更改后处理项目的删除。 I've made an example here: http://jsfiddle.net/mkjj94m1/2/ 我在这里做了一个例子: http : //jsfiddle.net/mkjj94m1/2/
I've renamed the function to deleteItem
as remove
is an existing jQuery function. 我将函数重命名为deleteItem
因为remove
是现有的jQuery函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.