开发者

Is the data stored with jquery's data method removed when the element is removed?

What happens to the data stored for a given element using the jQuery.data() function after that element is removed or replaced? In tests it appears this data is no long开发者_运维技巧er available after the element it was attached to is removed.


Yes, when an element is removed by .remove() or .empty() on a parent (or .html() on a parent), the data (stored in the jQuery.cache object) is cleaned up as well.

You can see in the source:

  • Here's where it happens for .remove()
  • Here's where it happens for .empty()
  • Here's where it happens for .html()

In all these cases, the jQuery.cleanData() function does the work.


In case others finding this are looking for ways to directly remove data, there are functions for this, just not commonly used directly: .removeData() and $.removeData(). They behave like .data() and $.data() in usage - without the data key (name parameter) they'll clear all data values for the element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜