如何使用 jQuery 克隆触摸事件?
我需要一些关于 jQuery 中的 clone()
的帮助。
所以,事情是这样的:我正在做某种适用于触摸设备的拖放和排序。一切都很好,除了当我克隆一个元素时,事件消失了(拖动停止工作)。
代码看起来像这样(过于简化):
$('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
});
});
我做错了什么?
谢谢!
I kind of need some help with clone()
in jQuery.
So, the thing is this: I'm doing some kind of drag&drop&sort that will work on touch devices. Everything is good except that when i clone an element, the events are gone (drag stop working).
The code looks like this (over simplified):
$('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
});
});
What do i do wrong?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 DOM 元素是一回事(并且不会自动克隆),因此使用像
insertBefore
这样的函数将移动该元素而不是复制它。如果您使用这个“技巧”,您也不会遇到事件不被克隆的问题,因为它是绑定了所有内容的相同元素。所以你可以这样做,例如:
Since a DOM element is one thing (and not automatically cloned), using a function like
insertBefore
will move the element instead of copying it. If you make use of this 'trick', you also don't have trouble with events not being cloned because it's the very same element with everything bound.So you could do e.g.: