jQuery事件多次触发

发布于 2025-01-23 23:56:41 字数 1058 浏览 1 评论 0原文

我的divs克隆了,其中包含将父母克隆的按钮。尝试单击按钮后,事件被触发2n次,以便单击第二个克隆会产生其他4个克隆,依此类推:

$(".add").off("click").on("click", function(e) {
  e.stopPropagation();
  e.stopImmediatePropagation();
  $(".cloneable").clone(true).appendTo(".container");
});

$(".rem").off("click").on("click", function() {
  if (document.getElementsByClassName('container')[0].childElementCount > 1) {
    $(".cloneable:last").remove();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary rem" role="button">Remove cell</a>
    </div>
    <iframe src="index.php"></iframe>
  </div>
</div>
What could I be doing wrong?

I have cloned divs containing buttons that clone their parents. Upon trying to click the buttons, events are triggered 2n times such that clicking the second clone produces 4 other clones and so on:

$(".add").off("click").on("click", function(e) {
  e.stopPropagation();
  e.stopImmediatePropagation();
  $(".cloneable").clone(true).appendTo(".container");
});

$(".rem").off("click").on("click", function() {
  if (document.getElementsByClassName('container')[0].childElementCount > 1) {
    $(".cloneable:last").remove();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary rem" role="button">Remove cell</a>
    </div>
    <iframe src="index.php"></iframe>
  </div>
</div>

What could I be doing wrong?

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

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

发布评论

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

评论(2

執念 2025-01-30 23:56:41

几件事。

  1. 使用递延点击处理程序更容易。
  2. 您正在克隆所有“可克隆”,而不仅仅是first。 jQuery会找到所有可克隆,而不仅仅是您的原始内容。
  3. 现在删除您单击的那个,而不是最后一个。
  4. 禁用的类将被添加/删除到rem按钮,具体取决于是否仅剩下一个。
$(".container").on("click", ".add", function(e) 
{
  e.stopPropagation();
  e.stopImmediatePropagation();
  $(".container .rem").removeClass("disabled");
  
  let $cloneable = $(".cloneable").first().clone(true);
  $cloneable.appendTo(".container");
  
}).on("click", ".rem", function() 
{
  if($(this).hasClass("disabled") ) return;
  
  $(this).closest(".cloneable").remove();
  
  if($(".container .rem").length === 1)
    $(".container .rem").addClass("disabled");  
});
.btn.btn-primary
{
  background:#d5d5d5;padding:4px;cursor:pointer;
}

.btn.btn-primary.disabled
{
  background:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary disabled rem" role="button">Remove cell</a>
    </div>
    <iframe src="trial37.php"></iframe>
  </div>
</div>

A few things.

  1. It's easier to use a deferred click handler.
  2. You were cloning all 'cloneable', not just the first. jQuery will find all cloneable and clone them, not just your original.
  3. Remove now removes the one you clicked on, not the last one.
  4. The disabled class will be added/removed to the rem button, depending on if there is only one left or not.

$(".container").on("click", ".add", function(e) 
{
  e.stopPropagation();
  e.stopImmediatePropagation();
  $(".container .rem").removeClass("disabled");
  
  let $cloneable = $(".cloneable").first().clone(true);
  $cloneable.appendTo(".container");
  
}).on("click", ".rem", function() 
{
  if($(this).hasClass("disabled") ) return;
  
  $(this).closest(".cloneable").remove();
  
  if($(".container .rem").length === 1)
    $(".container .rem").addClass("disabled");  
});
.btn.btn-primary
{
  background:#d5d5d5;padding:4px;cursor:pointer;
}

.btn.btn-primary.disabled
{
  background:red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <div>
      <a class="btn btn-primary add" role="button">Add cell</a>
      <a class="btn btn-primary disabled rem" role="button">Remove cell</a>
    </div>
    <iframe src="trial37.php"></iframe>
  </div>
</div>

水水月牙 2025-01-30 23:56:41
$(".add").on("click", function(e) {  $(this).parent(".cloneable").clone(true).appendTo(".container");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <a class="btn btn-primary add" role="button">Add cell</a>
    <div>CONTENT</div>
  </div>
</div>

因此,方法中正在发生的事情是,您每次单击时都在进行新的查询,并且它返回所有.clonable elements,然后再克隆。您想要的只是找到与您单击的项目相关的一个,因此您要以$(this)开始,这是单击的项目,然后您可以使用parent( )沿着DOM树寻找选择器的方法。

在这种情况下,可以在单击事件中放置调试器;,然后一次执行方法。像$('。clonable')。然后,您可以看到它每次都会变得更大,并且它们指出了所有现有元素。

$(".add").on("click", function(e) {  $(this).parent(".cloneable").clone(true).appendTo(".container");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
  <div class="cloneable">
    <a class="btn btn-primary add" role="button">Add cell</a>
    <div>CONTENT</div>
  </div>
</div>

So, what's happening in your method is that you are doing a new query each time you click and it returns all the .clonable elements and then clones them. What you want is to only find the one relevant to the item you clicked on, so you want to start with $(this) which is the item clicked and then you can use the parent() method to go up the DOM tree looking for the selector.

In these sorts of situations, it can help to put a debugger; inside your click event and then execute the method one at a time. Like $('.clonable'). You can then see maybe that it gets larger every time and that they point to all the existing elements.

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