gpt4 book ai didi

javascript - jquery拖放问题

转载 作者:行者123 更新时间:2023-12-03 12:09:43 25 4
gpt4 key购买 nike

我有下面的代码,使用我正在制作可拖动和可放置的东西的脚本。目前我有两 block 可拖动内容,如果正确的话可以将它们放在两个地方,然后系统将更改并说正确,我想知道是否有办法让我能够只创建 HTML div 并运行代码并将可拖动对象与可放置对象进行匹配。我有人有非技术背景,有基本的 HTML 知识,所以你现在如何添加 div 和删除它们,但他们能够处理脚本,我想知道,如果我的 ID 为 1-10 Draggable 内容与 dropable 1-10 相同,因此 id 1draggable 只能添加到 id 1 droppable。

<meta charset="utf-8">
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<link rel="stylesheet" href="hhtps:/resources/demos/style.css">
<style>
#droppable,#droppable2 { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
#draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
</style>
<script>
$(function() {
$( "#draggable, #draggable2" ).draggable();


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

$( "#droppable2" ).droppable({
accept: "#draggable2",
drop: function( event, ui ) {
$( this )
.removeClass("ui-widget-header")
.addClass( "ui-state-highlight" )
.find( "p" )
.html( "Dropped!" );
},
out: function( event, ui ) {
$( this ).removeClass( "ui-state-highlight" ).addClass( "ui-widget-header" )
.find( "p" )
.html( "accept" );
}
});

});
</script>

<div id="draggable2" class="ui-widget-content">
<p>I'm draggable but can't be dropped</p>
</div>

<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
<p>accept: '#draggable'</p>
</div>

<div id="droppable2" class="ui-widget-header">
<p>accept: '#draggable2'</p>
</div>

最佳答案

为此,您需要避免使用 HTML id 并开始使用类。以下是如何做到这一点,使用 working example :

您的 HTML:

<div id="draggable_2" class="ui-widget-content draggable-item">
<p>draggable_2</p>
</div>
<div id="draggable" class="ui-widget-content draggable-item">
<p>draggable</p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable">
<p></p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable_2">
<p></p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable_3">
<p></p>
</div>

您的 JavaScript:

$(function () {
$(".draggable-item").draggable();

$('.droppable-item').each(function (i, ele) {
// Gets the accepted from the HTML property "data-accept"
var accept = $(this).data('accept');

// This is just to show what the item accepts. you can remove it.
$(this).find('p').text('accepts: ' + accept);

// Init the jQuery UI droppable()
$(this).droppable({
accept: accept,
drop: function (event, ui) {
$(this)
.removeClass("ui-widget-header")
.addClass("ui-state-highlight")
.find("p")
.html("Dropped!");
},
out: function (event, ui) {
$(this).removeClass("ui-state-highlight").addClass("ui-widget-header")
.find("p")
.html("accept: " + accept);
}
});
});
});

关于javascript - jquery拖放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25019149/

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