开发者

Move jQuery data() when an element is destroyed and re-created

开发者 https://www.devze.com 2022-12-25 06:06 出处:网络
Can anyone thi开发者_如何学编程nk of a (preferably quick) way to move the data() attached to a DOM element to a new instance of itself?

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.

0

精彩评论

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