当替换元素时,事件和数据发生了什么 [英] When replace element, what happen on the events and data

查看:84
本文介绍了当替换元素时,事件和数据发生了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道,当Ajax加载时替换了A元素时,只是被断开连接还是直接删除了对它视而不见的所有事件和数据?

I curious to know that when A element has been replaced when ajax load, all the events and data blinded on it will be just disconnected or removed directly?

我不相信任何一旦被替换掉的事件和数据都不会维持在 $(parent),$(document)或$(window)内.如果是这样,它们只是被断开连接还是被卸下?

I dont believe any events and data not blinded trough $(parent), $(document) or $(window) will maintain if once has been replaced. If so, are they just been disconnected or removed?

推荐答案

如果事件直接绑定到某个元素(不使用委托事件处理),那么当该元素被删除时,它也是事件处理程序.

If an event is bound directly to an element (not with delegated event handling), then when that element is removed, so will it's event handler.

类似地,如果已将任何属性或属性添加到该DOM元素,则它们与该特定DOM元素一起存在,因此,如果删除了DOM元素,它们也会被删除.

Similarly, if any properties or attributes have been added to that DOM element, they go with that particular DOM element so if the DOM element is removed so are they.

如果使用委派事件处理(监视气泡上升到更高级别的父对象(如 body document )的事件,则这些事件仍将保留因为事件实际附加到的对象没有被替换.

If one is using delegated event handling (watching for events that bubble up to a higher level parent object like body or document, then those events will still be in place because the objects the events are actually attached to did not get replaced.

您的问题没有用jQuery标记,但是您使用了一些类似jQuery的示例,因此在jQuery世界中,事件附加如下:

Your question isn't tagged with jQuery, but you use some jQuery-like examples so in the jQuery world, an event attached like this:

$("#myObject").on('click', fn);

当替换 #myObject 时将被删除.但是,这样的委托事件:

Will be removed when #myObject is replaced. But, a delegated event like this:

$(document.body).on('click', '#myObject', fn);

即使将 #myObject 替换为具有相同ID的其他对象,

仍将生效,因为该事件实际附加到的对象( document.body 在这种情况下)没有被移除或替换.

Will still be in force, even if #myObject was replaced with a different object with the same ID because the object that the event is actually attached to (document.body in this case) did not get removed or replaced.

这篇关于当替换元素时,事件和数据发生了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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