[英]How to dynamically generate a div and make it dragable
I am trying to create dynamic divs
onClick
of a button, but I also want to make the newly added divs
to be draggable
. 我正在尝试创建按钮的动态
divs
onClick
,但我也想使新添加的divs
可draggable
。 How I can achieve this functionality? 如何实现此功能?
HTML HTML
Drag me around 拖我
Drag me around 拖我
Drag me around 拖我
$(function() { $( "#draggable" ).draggable(); $( "#draggable-2" ).draggable(); $( "#draggable-3" ).draggable(); });
What I want to do is that I need to create the divs dynamically on a click and apply the same draggable
functionality to them 我想做的是,我需要在单击时动态创建div并将相同的
draggable
功能应用于它们
I think you just have to append a new div to your container and make it draggable. 我认为您只需要在容器中添加一个新的div并使其可拖动即可。
var nDraggable = 3;
$('#yourButton').click(function(){
nDraggable++;
$("#container").append('<div id="draggable-'+nDraggable'">Drag me around</div>');
$("#draggable-"+nDraggable).draggable();
});
Is this what are you looking for? 这是你要找的东西吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.