复制DOM元素及其事件在变量(jQuery) [英] Copy DOM element with its events in variable (jQuery)

查看:208
本文介绍了复制DOM元素及其事件在变量(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在变量中复制DOM元素,所以我这样做:

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


var before = $(#someid)。 html();

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

然后我的脚本在这个someidDOM中做了一堆东西,之前是:

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);

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

这个工作确定,但问题是,我有一些事件在这个DOM和那些事件不能像这样复制...所以有另一种方法吗? p>

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?

推荐答案

clone()方法可以保留事件处理程序和元素数据。您可以写:

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

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

稍后:

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

这篇关于复制DOM元素及其事件在变量(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆