gpt4 book ai didi

jquery - 一旦掉落的元素数量达到一定限制,就避免掉落

转载 作者:行者123 更新时间:2023-12-01 04:44:52 24 4
gpt4 key购买 nike

我正在使用 Jquery UI Draggable。我试图避免一旦接收该项目的 div 达到限制,项目就会被丢弃。正如您在下面看到的,这是我的代码。

 $( "#grid-list" ).droppable({
over: function(event, ui) {
count = 0;
$("#grid-list").find("li").each(function () {
count++;

if(count > $("#grid-list").data("albumlist")){
alert("limit reached");
}
});
}
});

JS fiddle 示例:http://jsfiddle.net/r1Lnxby9/

最佳答案

编辑、更新

尝试从 htmldrag 中删除 ondragstart="drag(event)"draggable="true" > 未出现在 js 处定义;在 .droppable() options 处设置 tolerance:"touch" ;将 $("> li", this).length 替换为 count$("#grid-list").each()>= 对于 > 如果预期结果是 .droppable() 元素内的 1 元素最大值;在over事件

内调用 event.preventDefault()event.stopPropagation()

  $("#list-albuns, #grid-list").sortable({
connectWith: ".connectedSortable"
}).disableSelection();

$("#grid-list").droppable({
tolerance: "touch",
over: function(event, ui) {
if ($("> li", this).length >= $(this).data("albumlist")) {
event.preventDefault();
event.stopPropagation();
alert("limit");
}
}
});
ul li {
display: inline-block;
list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
</script>

<ul id="list-albuns" class="connectedSortable ui-sortable">
<li id="0" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="0">
<img id="dragimg0" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/51552793.png" alt="Appetite for Destruction">
</li>
<li id="2" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="2">
<img id="dragimg2" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/97176319.png" alt="Use Your Illusion II">
</li>
<li id="3" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="3">
<img id="dragimg3" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/89710469.png" alt="40 Seasons - The Best Of Skid Row">
</li>
<li id="5" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="5">
<img id="dragimg5" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/56005467.jpg" alt="Danger Danger">
</li>
<li id="6" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="6">
<img id="dragimg6" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/85595741.jpg" alt="Screw It [Italy Bonus Tracks]">
</li>
<li id="7" class="ui-sortable-handle">
<input type="hidden" name="albumId[]" value="7">
<img id="dragimg7" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/90791541.jpg" alt="Detonator">
</li>
</ul>Drop Here:
<ul id="grid-list" data-albumlist="1" class="connectedSortable" style="width:500px; height:200px;display:block; border:2px solid black;"></ul>

jsfiddle http://jsfiddle.net/r1Lnxby9/2/

关于jquery - 一旦掉落的元素数量达到一定限制,就避免掉落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105873/

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