Copy DOM element with its events in variable (jQuery)

馋奶兔 提交于 2019-12-04 20:11:29

问题


I wanted to copy DOM element in variable, so I did this:

var before=$("#someid").html();

Then my script does a bunch of stuff in this "someid" DOM and after that is completed I restored DOM like it was before:

$("#someid").html(before);

This works ok but the problem is that I had some events in this DOM and those events can not be copied like this... So is there another way to do this?


回答1:


The clone() method can preserve both event handlers and element data. You can write:

var $before = $("#someid").clone(true);

Then later:

$("#someid").replaceWith($before);


来源:https://stackoverflow.com/questions/5778166/copy-dom-element-with-its-events-in-variable-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!