以下是我从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()
? - techfoobar