拖动后无法单击的元素

发布于 2025-02-03 12:22:11 字数 990 浏览 2 评论 0原文

我的左侧有一些盒子。当它们被拖动时,它们会被克隆,因此您可以将任意数量的盒子放在想要的盒子上。

当它们被拖动到.sortable时,我希望通过单击链接的x来将它们删除。

<div class="draggable">
  <div class="structure">
    <div class="item1 e1" data-class="e1" data-parent="w1">New block <a href="javascript:;" class="remove">X</a></div>
    <div class="item1 e2" data-class="e2" data-parent="w2">New headline <a href="javascript:;" class="remove">X</a></div>
  </div>
</div>

<div class="sortable"></div>

这是我的脚本:

$(".sortable").sortable();

$(".structure .item1").draggable({
  connectToSortable: ".sortable",
  helper: "clone"
});

$(".remove").on("click", function () {
  alert("Hello out there!");
});

不幸的是,此x似乎无能为力。什么都没有发生。你知道我做错了什么吗?

在这里,您可以找到一个演示

I got some boxes on the left that are draggable. When they are dragged, they are cloned, so you can put as many boxes as you want.

When they are dragged to the .sortable I want them to be removed by clicking on the x of a link.

<div class="draggable">
  <div class="structure">
    <div class="item1 e1" data-class="e1" data-parent="w1">New block <a href="javascript:;" class="remove">X</a></div>
    <div class="item1 e2" data-class="e2" data-parent="w2">New headline <a href="javascript:;" class="remove">X</a></div>
  </div>
</div>

<div class="sortable"></div>

This is my Script:

$(".sortable").sortable();

$(".structure .item1").draggable({
  connectToSortable: ".sortable",
  helper: "clone"
});

$(".remove").on("click", function () {
  alert("Hello out there!");
});

Unfortunately this x doesn't seem to do anything. Nothing happens. Do you have an idea what I'm doing wrong?

Here you find a demo https://codepen.io/alphafrau/pen/WNMJpow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

述情 2025-02-10 12:22:11
$(function() {
  $(".sortable").sortable();

  $(".structure .item1").draggable({
    connectToSortable: ".sortable",
    helper: "clone"
  });

  $(document).on("click", ".remove", function() {
    if (confirm("Are you sure you want to remove this element?")) {
      $(this).parent().remove();
      $(".sortable").sortable("refresh");
    }
  });
});
.item1 {
  border: 1px solid #ccc;
  width: 150px;
  height: 50px;
  padding: 0.5em;
}

.sortable {
  border: 1px solid #222;
  padding: 3px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="draggable">
  <div class="structure">
    <div class="item1 e1" data-class="e1" data-parent="w1">New block <a href="javascript:;" class="remove">X</a></div>
    <div class="item1 e2" data-class="e2" data-parent="w2">New headline <a href="javascript:;" class="remove">X</a></div>
  </div>
</div>

<div class="sortable"></div>

这是使用更正的事件委托。

https://api.jquery.com/on/

提供选择器时,事件处理程序称为委派。当事件直接出现在绑定元素上时,而是针对匹配选择器的后代(内部元素)时,没有调用处理程序。 jQuery从事件目标到附加处理程序的元素(即,最内在到最外面的元素)将事件冒泡。

$(function() {
  $(".sortable").sortable();

  $(".structure .item1").draggable({
    connectToSortable: ".sortable",
    helper: "clone"
  });

  $(document).on("click", ".remove", function() {
    if (confirm("Are you sure you want to remove this element?")) {
      $(this).parent().remove();
      $(".sortable").sortable("refresh");
    }
  });
});
.item1 {
  border: 1px solid #ccc;
  width: 150px;
  height: 50px;
  padding: 0.5em;
}

.sortable {
  border: 1px solid #222;
  padding: 3px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
<div class="draggable">
  <div class="structure">
    <div class="item1 e1" data-class="e1" data-parent="w1">New block <a href="javascript:;" class="remove">X</a></div>
    <div class="item1 e2" data-class="e2" data-parent="w2">New headline <a href="javascript:;" class="remove">X</a></div>
  </div>
</div>

<div class="sortable"></div>

This is using the corrected event delegation.

https://api.jquery.com/on/

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文