这是我的小提琴。
在 jqueryUI 可拖动属性中,如何根据要删除元素的元素动态设置 appendTo 目标?
HTML:
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
<div id="assets">
<h4>Drag these to the red box</h4>
<div class="asset">A1</div>
<div class="asset">A2</div>
<div class="asset">A3</div>
<div class="asset">A4</div>
</div>
CSS:
#header, #content, #footer {
height: 150px;
width: 50%;
border: 1px solid red;
position: relative;
float: left;
margin: 20px;
}
#assets {
position: fixed;
top: 0;
right: 0;
}
.asset {
border: 1px solid blue;
width: 50px;
height: 50px;
}
.drop {
background-color: pink;
}
JavaScript:
$('.asset').draggable({
/*If we change appendTo to our #target dropzone, it works fine*/
appendTo: 'body', //Try it with either #header, #content, #footer
helper: 'clone'
});
$("#header, #content, #footer").droppable({
hoverClass: "drop",
drop: function (event, ui) {
console.log('dropped');
var element = $('<div class="asset">' + ui.draggable.text() + '</div>');
element.css('top', ui.position.top);
element.css('left', ui.position.left);
element.css('position', 'absolute');
$(element).appendTo(this);
}
});