2011-08-17 210 views
1

我需要一些在jQuery中使用clone()的帮助。如何用jQuery克隆touchevents?

所以,事情是这样的:我正在做某种拖动&下降&排序,将在触摸设备上工作。一切都很好,除了当我克隆一个元素时,事件不见了(拖动停止工作)。

的代码看起来像这样(在简体):

$('li', layers).each(function(){ 
    this.addEventListener('touchend', function(e){ 
     var cloned = $(this).clone(true, true); // no events are cloned at all! 
     cloned.insertBefore(this.helper); 

    }); //ontouchend 

    this.addEventListener('touchmove', function(e){ 
     // do some stuff with this.helper 
    }); 
}); 

我该怎么办错了吗?

谢谢!

+1

你也删除`touchend`原?如果是这样,你可以试试`this.insertBefore(...)`;它会自动移动。 – pimvdb 2011-08-17 11:17:41

回答

1

由于DOM元素是一件事情(而不是自动克隆),使用像insertBefore这样的函数将移动元素而不是复制它。如果你使用这个“诡计”,你也不会遇到没有被克隆的事件的麻烦,因为它是所有绑定的元素。

所以,你可以做例如为:

this.addEventListener('touchend', function(e){ 
    this.insertBefore(this.helper); 
}); //ontouchend