Moving an item programmatically with jQuery sortable while still triggering events

前端 未结 4 1895
Happy的楠姐
Happy的楠姐 2020-12-05 06:20

I am using jQuery Sortable. I have the HTML setup like so:

  • 1
  • 2
4条回答
  •  死守一世寂寞
    2020-12-05 07:00

    The trigger method accepts extra parameters, so if you need the ui parameter, you’ll need to pass that in yourself. But you usually just want the target ui.item, and in this case, you know what that is:

    var sortupdate = function (event, ui) {
        // do something with ui.item
    };
    
    var $plan = $("#plan");
    $plan.sortable({
        update: sortupdate
    });
    $plan.on("sortupdate", sortupdate); // sortupdate is not automatically bound
    
    var $item = $("#plan li:eq(1)");
    $item.insertAfter($("#plan li:eq(2)"));
    $plan.trigger("sortupdate", { item : $item });
    

    Demo:

    http://jsfiddle.net/D7fCz/3/

提交回复
热议问题