开发者

Where does jQuery data's method information go?

Can I do

<div class="some_div">some div</div>
<script>
    jQuery('.some_div').data('some_data','some info').remove();
开发者_如何学编程</script>

where the information is added with data's method, is still around the DOM? Do I need to uninitialize? Is the information automatically removed when the div element has no references left?


The data is stored in a variable available to the jQuery objects via closure. It is never stored in the dom. Remove method deletes the data along with the DOM element.

.remove( [ selector ] )

Similar to .empty(), the .remove() method takes elements out of the DOM. We use .remove() when we want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

source: jQuery API: remove()


jQuery places a serial number on DOM elements when needed, and uses that reference to look up associated data for the element.

For example: jQuery1278101043588: 1

As long as the element exists, your data should exist. No references to the element in code are needed.

The data associated with an element is cleaned up when you call .remove() or .empty().

If you wish to remove an element from the DOM without losing the data, you can use .detach().

  • http://api.jquery.com/remove/
  • http://api.jquery.com/empty/
  • http://api.jquery.com/detach/

jQuery also has a .removeData() method for clearing data that is no longer needed. It will clear all data on the element(s) if called without passing an argument.

  • http://api.jquery.com/removeData/
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜