gpt4 book ai didi

jQuery droppable - 在给定位置触发给定元素的放置

转载 作者:行者123 更新时间:2023-12-01 04:08:39 25 4
gpt4 key购买 nike

如何动态创建 div 并将其放入可放置的 div 中?

这是实际的代码。

   <head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
#draggable { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
#droppable { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
</style>
<script>
$(function() {
$( "#draggable" ).draggable();
$( "#droppable" ).droppable({
drop: function( event, ui ) {
$( this )
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
}
});
});
</script>
</head>
<body>
<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
</body>

现在我正在创建一个新元素

$("body").append("<div id='custom_1'></div>")
$("#custom_1").draggable()

如何以编程方式将 #custom_1 放入给定位置的 #droppable 中?

我尝试过类似的方法,但没有成功

$("#droppable").trigger('drop',$('custom_1'))

注意:

我想触发事件(这样我就可以获取它们的参数eventui),而不是要求它只执行drop中的代码事件。

最佳答案

无需重写事件链,您可以使用 jQuery.simulate plugin 。 jQuery UI 开发团队使用它进行 jQuery UI 单元测试。

代码:

$(function () {
$("#draggable").draggable();
$("#droppable").droppable({
drop: function (event, ui) {
$(this)
.addClass("ui-state-highlight")
.find("p")
.html("Dropped!");
}
});

$("body").append("<div id='custom_1' class='ui-widget-content'>demo</div>");
$("#custom_1").draggable();
var destination = $('#droppable').offset();

$("#custom_1").simulate("drag", {
dx: -destination.left + 50, // move to this x
dy: -destination.top + 20, // move to this y
speed: 5000 // set speed
});
});

演示:http://jsfiddle.net/IrvinDominin/ALcC4/

关于jQuery droppable - 在给定位置触发给定元素的放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23882775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com