【发布时间】:2016-11-25 14:01:33
【问题描述】:
我有一些带有可放置类“dropBox”的jQuery div;我想接受带有“teamBox”类的 div,但前提是它们不存在于可放置的“dropBox”容器中。我尝试通过对 dropBox 使用 over 事件来执行此操作,但发现检查 dropBox 是否包含可拖动元素始终返回 true。我还尝试在 dropBox 的 accept 部分放置一个条件,但我遇到了同样的问题。以下两种尝试的代码。谢谢!
HTML
<div class="panel-heading">Teams</div>
<div class="panel-body teamContainer">
@foreach($teamContent as $teamID => $teamName)
<div id="{{ $teamID }}" class="teamBox">
<h5><b>{{ $teamName }}</b></h5>
</div>
@endforeach
</div>
<div class="panel-heading">Twitter</div>
<div class="panel-body">
@if(!empty($twitterContent))
@foreach($twitterContent as $twitterID => $screenName)
<span class="{{ $twitterID }}">
<h5>{{ '@' . $screenName }}</h5>
</span>
<div id="{{ $twitterID }}" class="dropBox"></div>
@endforeach
@endif
</div>
<div class="panel-heading">Facebook</div>
<div class="panel-body">
@if(!empty($facebookContent))
@foreach($facebookContent as $pageID => $pageName)
<span class="{{ $pageID }}">
<h5>{{ $pageName }}</h5>
</span>
<div id="{{ $pageID }}" class="dropBox"></div>
@endforeach
@endif
</div>
使用over的jQuery尝试
$(".teamBox").draggable({
helper: 'clone',
zIndex: 1,
});
$(".dropBox").droppable({
accept: '.teamBox',
over: function(event, ui) {
var draggable = ui.draggable;
var over = $(this);
if($(over).find($(draggable).attr('id')))
{
//Always logs as disabling
console.log("Disabling");
$(over).droppable('disable');
}
},
drop: function(event, ui) {
var dropped = ui.draggable;
var droppedOn = $(this);
$(this).append($(dropped).clone());
$(".dropBox .teamBox").addClass("item");
$(".item").removeClass("ui-draggable");
$(".item").draggable({
zIndex: 1,
});
}
});
使用accept的jQuery尝试
$(".teamBox").draggable({
helper: 'clone',
zIndex: 1,
});
$(".dropBox").droppable({
accept: function(draggable) {
if($(this).has($(draggable).attr('id'))) {
//Always returns false
console.log("Returning false");
return false;
}
return true;
},
drop: function(event, ui) {
var dropped = ui.draggable;
var droppedOn = $(this);
$(this).append($(dropped).clone());
$(".dropBox .teamBox").addClass("item");
$(".item").removeClass("ui-draggable");
$(".item").draggable({
zIndex: 1,
});
}
});
编辑 最终想出了一个对我有用的解决方案。这是a Fiddle。请记住,我使用的是 Laravel Spark,因此 HTML 中的 Laravel 刀片语法在 Fiddle 中无法正确编译。但是,查看代码可能会让您了解如何创建具有克隆和拒绝重复可拖动项目的拖放系统。
【问题讨论】:
标签: javascript jquery laravel jquery-ui jquery-ui-draggable