Move jQuery data() when an element is destroyed and re-created
Can anyone thi开发者_如何学编程nk of a (preferably quick) way to move the data() attached to a DOM element to a new instance of itself?
The lightbox plugin I'm using deletes and re-appends and element to the page in order to display it in the lightbox (to aviod the multiple-ids issue that ASP.net has), and obviously the .data() that is attached to the element is lost when this happens.
There's a relatively new overload for .clone()
you can use to do this.
.clone(true)
will copy the element with events and data intact.
Alternatively, change your plugin to use .detach()
rather than .remove()
which keeps data intact. From the docs:
The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.
精彩评论