首页 > 解决方案 > 如何移动附加的 div?

问题描述

我有这个代码:

<div id="plan">
    <div id="" class="move circle pe">1</div>
</div>

$('.move').draggable({
      containment: '#plan',
      stop: function(e, ui) {
        var offset = $(this).offset(),
          x = offset.left,
          y = offset.top;
        //callback will be here
        alert("posição x: " + x + " posição y:" + y);
      }
    });

我可以移动带有数字 1 的 div,但是当我生成一个新的 div

function createStation(station){
        number = window.prompt("Escolha o numero para o posto");
        id = station+number;
        $("#plan").append("<div id='"+id+"' class='move circle pe'>"+number+"</div>");
    }

div 出现,但我无法移动它。我需要让它移动但也要提醒它的位置(未来回调)

请检查jsfiddle:

https://jsfiddle.net/mornaistar/q180z7Ls/11/

标签: jquery-ui-draggable

解决方案


我会为绑定创建一个额外的函数,例如draggable()在你重新绑定类的每个新项目movedraggable()

添加了一个功能dragger()

function dragger() {
    $('.move').draggable({
        containment: '#plan',
        stop: function(e, ui) {
            var offset = $(this).offset(),
                x = offset.left,
                y = offset.top;
            //callback para salvar em php
            alert("posição x: " + x + " posição y:" + y);
        }
    });
}

小提琴示例:https ://jsfiddle.net/2f4c36v7/


推荐阅读