Copy DOM element with its events in variable (jQue

2020-06-16 04:17发布

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条回答
可以哭但决不认输i
2楼-- · 2020-06-16 05:11

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

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

Then later:

$("#someid").replaceWith($before);
查看更多
登录 后发表回答