【发布时间】:2013-07-30 07:02:41
【问题描述】:
这是我来自http://jsfiddle.net/XUFUQ/1/的代码
$(document).ready(function()
{
addElements();
}
);
function addElements()
{
$("#list1").empty().append(
"<li id='item1' class='list1Items'>Item 1</li>"+
"<li id='item2' class='list1Items'>Item 2</li>"+
"<li id='item3' class='list1Items'>Item 3</li>"
);
}
$(function() {
// there's the gallery and the trash
var $gallery = $( "#list1" ),
$trash = $( "#list2" );
// let the gallery items be draggable
$( "li", $gallery ).draggable({
cancel: "button", // these elements won't initiate dragging
revert: "invalid", // when not dropped, the item will revert back to its initial position
containment: "document",
helper: "clone",
cursor: "move"
});
// let the trash be droppable, accepting the gallery items
$trash.droppable({
accept: "#list1 > li",
drop: function( event, ui ) {
$("#list2").append(ui.draggable);
addElements();
}
});
});
在文档就绪方法中,我将一些元素附加到 list1,然后我在该列表上初始化可拖动,因此我第一次能够拖动 list1 元素。在放入 list2 时,我正在调用 addElements() 函数来清除并将一些元素添加到 list1。但我无法拖动这些添加的元素。
如何使这些元素可拖动?
【问题讨论】:
-
大概用on(),但是没有代码就不好说了。
-
每次创建新元素时,您只需将
draggable绑定到它们。 -
抱歉耽搁了,我不明白如何发布链接,请通过上面的链接
-
@Tdelang - 如何将
.on()与可拖动一起使用? -
@Tdelang .on() 用于事件监听器,而不是插件初始化
标签: jquery draggable jquery-ui-draggable