使用JQuery,当DOM元素调用.remove()时,是否可以运行一个函数? [英] With JQuery, is it possible to have a function run when a DOM element calls .remove()?
本文介绍了使用JQuery,当DOM元素调用.remove()时,是否可以运行一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我查看了卸载,但从我的理解,只有当浏览器从页面导航时才会调用。
提前感谢!
解决方案
我不知道这是你想要的。不是真的很漂亮的代码,只是为了显示我想要使用的:
//只是为了这个脚本的缘故,你会想做不同的
var body = $(body);
var element = $('#loadingBlock');
//绑定析构函数触发事件
body.bind(elementDeleted,function(element){
//您的析构函数代码
} );
//触发事件,删除元素,可以放在函数中,重载等
body.trigger(elementDeleted,element);
element.remove();
当然解决方案基于直接观看DOM,但问题是浏览器的兼容性。您应该查看突变事件。
As the question states, what I'm attempting to do is have a function that is called when a DOM element is removed from the DOM, much like a destructor.
I looked into unload, but from my understanding that's only called when the browser navigates away from the page.
Thanks in advance!
解决方案
I don't know if it's that what you're looking for. Not really pretty code, just to show what I would like to use:
// Just for this script's sake, you'll want to do it differently
var body = $("body");
var element = $('#loadingBlock');
// Bind the "destructor" firing event
body.bind("elementDeleted", function (element) {
// your "destructor" code
});
// Trigger the event, delete element, can be placed in a function, overloaded, etc.
body.trigger("elementDeleted", element);
element.remove();
There are of course solutions based on watching the DOM directly but the problem is the browser compatibility. You should probably check out mutation events.
这篇关于使用JQuery,当DOM元素调用.remove()时,是否可以运行一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文