将DOM元素复制到变量(jQuery)中 [英] Copy DOM element with its events in variable (jQuery)

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

问题描述

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

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


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

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

然后我的脚本在这个someidDOM中做了一堆东西,之后我完成了DOM的恢复之前:

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?

推荐答案

克隆( )方法可以保存事件处理程序和元素数据。你可以写:

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天全站免登陆