jquery克隆可拖动点击
我有一个类为 .myClass 的 div,我通过单击 #cloneIT 来克隆它 它完美地克隆了该 div,但是当我拖动它时,它总是拖动第一个 div。 这是我的代码:
$("document").ready(function(){
$(".myClass").draggable({ containment: 'parent' });
var countClones = 0;
$("#cloneIt").click(function(){
$('.myClass').clone(true).attr('id',$('div.myClass')[0].id+countClones).prependTo('body');
countClones += 1;
var newDiv = $("<div>Cloned: " + countClones + "</div>");
$("body").append(newDiv);
return false;
});
});
jsFiddle: http://jsfiddle.net/qQ6ws/3/
1) 当您单击克隆它时,请拖动测试,它将具有我无法单击或拖动的克隆版本。
2) 已在更新中修复
更新: http://jsfiddle.net/qQ6ws/3/
我已经解决了第二个问题,现在唯一的问题是我无法拖动克隆版本,甚至无法单击它们 感谢您的任何帮助。
I have a div with class .myClass, I am cloning it by clicking on #cloneIT
It clone that div perfectly but when I drag it always dragged first div.
here my code:
$("document").ready(function(){
$(".myClass").draggable({ containment: 'parent' });
var countClones = 0;
$("#cloneIt").click(function(){
$('.myClass').clone(true).attr('id',$('div.myClass')[0].id+countClones).prependTo('body');
countClones += 1;
var newDiv = $("<div>Cloned: " + countClones + "</div>");
$("body").append(newDiv);
return false;
});
});
jsFiddle:
http://jsfiddle.net/qQ6ws/3/
1)
When u click on Clone It, plz drag Test, It will have cloned version which I am not able to click or drag.
2) Fixed in update
UPDATE:
http://jsfiddle.net/qQ6ws/3/
I have fixed second issue now only problem is i can't drag cloned version or even click them
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这就是你想要的?即使将 clone(true) 作为参数传入,克隆可拖动元素似乎也不会克隆事件。我在其他 jQuery 控件(例如 droppable)中注意到了这一点。您必须在克隆元素上显式调用
.draggable()
。I guess this is what you want ? Cloning the draggable element doesn't seem to clone the events as well even with clone(true) passed in as the parameter. I have noticed this with other jQuery controls also such as droppable. You have to explicitly call
.draggable()
on the cloned element.我认为当您使用“true”参数进行克隆时,它还会复制事件处理程序,这会破坏“可拖动”实现。可能唯一的选择是在没有“true”参数的情况下进行克隆,并根据需要重新注册事件侦听器。
I think when you clone with "true" argument it also copies the event handlers, which breaks the "draggable"-implementation. Probably only option is to clone without the "true" argument and re-register the event listeners as needed.