Javascript 如何获取包含侦听器的元素的内容

Javascript 如何获取包含侦听器的元素的内容,javascript,jquery,dom,Javascript,Jquery,Dom,我现在的代码是 var existing = element.children().clone(true); element.empty(); // Do some stuff with element /* ... */ // Restore the previous content. element.empty().append(existing); 只要元素下不直接包含文本,这种方法就可以正常工作。我的意思是这样的: // Works great, because .children()

我现在的代码是

var existing = element.children().clone(true);
element.empty();
// Do some stuff with element
/* ... */
// Restore the previous content.
element.empty().append(existing);
只要元素下不直接包含文本,这种方法就可以正常工作。我的意思是这样的:

// Works great, because .children() will get tag2 and tag3, etc.
<element><tag2>...</tag2><tag3>...</tag3></element>

// Doesn't work, because .children() doesn't get the text node.
<element>some_text<tag2>...</tag2></element>
//效果很好,因为.children()将得到tag2和tag3等。
......
//不起作用,因为.children()不获取文本节点。
一些文字。。。
因此,我尝试使用
.contents()
,但是我的初始元素附加了一些事件(这就是为什么我在当前代码中使用
.clone(true)
),并且
.contents()
没有获得带有事件的元素

你知道我还能用什么方法吗?或者你能帮我写一些代码,做我想做的事情而不太复杂吗

非常感谢

编辑


我创建了一个this:向您展示。我希望当您单击“单击”时,它会显示警报消息。

与其尝试克隆事件侦听器,为什么不使用on()事件委派为您执行此操作


与其尝试克隆事件监听器,为什么不使用on()事件委派来为您克隆事件监听器呢

您应该使用.on()方法绑定事件。这样你就可以实现你想做的事情

 var existing = element.html();
 element.empty();
  // Do some stuff with element
 /* ... */
 // Restore the previous content.
 element.append(existing);
应该使用.on()方法绑定事件。这样你就可以实现你想做的事情

 var existing = element.html();
 element.empty();
  // Do some stuff with element
 /* ... */
 // Restore the previous content.
 element.append(existing);

我将委派事件。我将委派事件。我创建了一个fiddle:with
。on
它不起作用,我创建了一个fiddle:with
。on
它不起作用,因为我想创建一个fiddle:jsfiddle.net/Jv6rW with
。on
它不起作用,因为我想创建一个fiddle:jsfiddle.net/Jv6rW with
。在
上,它不能按我想要的方式工作