如何动态操作jquery可拖动UL元素的内容



我使用document.getElementById("myUL").innerHTML="<li...> 动态设置<ul>的innerHTML

我动态创建的<li>不可由jquery拖动。然而,静态HTML是.

如何使使用innerHTML指令创建的新创建的动态HTML可拖动?

这里有一些代码:

function onAddTask() {
    var task = document.getElementById("task").value;
    for (i = 0; i < MAX_TASKS; i++)  {
        if (tasks[i] == null) {
            tasks[i] = task;
            break;
        }
    }
    /* construct HTML */
    var html = "<li class='ui-widget-content ui-corner-tr'>" +
            "<h5 class='ui-widget-header'>New Task</h5>" +
            "<a href='' onclick='return onCreateTask();'><img src='graphics/newtask.png' width='96' height='72'/></a>" +
            "<center>Create a new task</center>" +
            "</li>";
    for (i = 0; i < MAX_TASKS; i++) {
        if (tasks[i] != null) {
            html += "<li class='ui-widget-content ui-corner-tr ui-draggable' style='width: 96px; display: list-item;'><h5 class='ui-widget-hea
                + "<img src=graphics/task.png width=96 height=72>"
                + tasks[i]+"</li>";
        }
    }
    var todoList = document.getElementById("todo");
    todoList.innerHTML = html;
    tb_remove();
    return false;
}

然后在HTML中我有

<body>
<ul id="todo" class="todo ui-helper-reset ui-helper-clearfix">
    <li class='ui-widget-content ui-corner-tr ui-draggable' style='width: 96px; display: list-item;'><h5 class='ui-widget-header'>blah</h5>
        <img src=graphics/task.png width=96 height=72>
    </li>
    <li class="ui-widget-content ui-corner-tr">
        <h5 class="ui-widget-header">New Task</h5>
        <a href="" onclick='return onCreateTask();'><img src="graphics/newtask.png" width="96" height="72"/></a>
        <center>Create a new task</center>
    </li>
    <li class="ui-widget-content ui-corner-tr">
        <h5 class="ui-widget-header">Task</h5>
        <img src="graphics/task.png" width="96" height="72" />
        <center>Task Data</center>
    </li>
</ul>

onAddTask()函数中,在tb_remove(); 行之前添加以下行

$("#todo li").draggable({
   helper : 'clone'
});

我假设所有必需的jquery+UI库都包含在内。希望能有所帮助。

最新更新