在特定父母div中重复隐藏div

发布于 2025-02-13 04:43:07 字数 917 浏览 1 评论 0原文

我在复制DIV并在其自己的父级中显示它有问题。我有几个divs带有相同的重复按钮。但是,该按钮仅在父母div中复制DIV。它不会在其自己的父级内显示DIV。

在我的示例中,我有四个按钮。单击其中一个时,应按下按钮下方显示一个div。但是,它只复制DIV并将其显示在其中一个按钮下方。我在做什么错?

$(document).ready(function() {
  $(".addB").click(function() {
    $(".newDiv")
      .eq(0)
      .clone()
      .insertAfter(".newDiv:last")
      .show();
  });

  $(document).on('click', '.addB button[type=submit]', function(e) {
    e.preventDefault()
    var $frm = $(this).closest('.addB');
    console.log($frm.serialize());
    $.ajax(
      $frm.attr('action'), {
        method: $frm.attr('method'),
        data: $frm.serialize()
      }
    );
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I have a problem with duplicating a div and displaying it within it's own parent div. I have several divs with the same duplication button. However, the button only duplicates the div in one of the parent divs. It does not display a div within its own parent div.

In my example I have four buttons. When clicking one of them there should be a div displaying underneath the button you press. However, it only duplicates the div and displays it underneath ONE of the buttons. What am I doing wrong?

https://jsfiddle.net/qer6c5b1/

$(document).ready(function() {
  $(".addB").click(function() {
    $(".newDiv")
      .eq(0)
      .clone()
      .insertAfter(".newDiv:last")
      .show();
  });

  $(document).on('click', '.addB button[type=submit]', function(e) {
    e.preventDefault()
    var $frm = $(this).closest('.addB');
    console.log($frm.serialize());
    $.ajax(
      $frm.attr('action'), {
        method: $frm.attr('method'),
        data: $frm.serialize()
      }
    );
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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

发布评论

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

评论(1

洛阳烟雨空心柳 2025-02-20 04:43:07

问题归因于您的固定.newdiv:last选择器 - 无论单击哪个按钮,这将始终针对相同的元素。

要解决此问题,您可以从传递给事件处理程序的事件中单击哪个按钮,然后使用DOM Traversal查找相关的.newdiv clone。

jQuery($ => {
  $(".addB").click(e => {
    let $btn = $(e.target);
    $btn.prev('.newDiv').clone().insertBefore($btn).show();
  });
});
.newDiv {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #1</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #2</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #3</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #4</p>
  </div>
  <button class="addB">+</button>
</div>

另请注意,我修改了parentDiv类在所有元素中很常见。这是因为增量classid属性是一种反模式,应避免。

最后,该逻辑假设每个按钮都在其父母内附加不同的内容。如果每次克隆的内容都是相同的,请克隆单个元素并从DOM中删除重复项,或使用&lt; template /&gt; < /code>保存要附加的内容。

The issue is due to your fixed .newDiv:last selector - this will always target the same element no matter which button is clicked.

To fix this you can determine which button was clicked from the Event that's passed to the event handler and then use DOM traversal to find the related .newDiv to clone.

jQuery($ => {
  $(".addB").click(e => {
    let $btn = $(e.target);
    $btn.prev('.newDiv').clone().insertBefore($btn).show();
  });
});
.newDiv {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #1</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #2</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #3</p>
  </div>
  <button class="addB">+</button>
</div>

<div class="parentDiv">
  <div class="newDiv">
    <p>This is the div i need to duplicate #4</p>
  </div>
  <button class="addB">+</button>
</div>

Also note that I amended the parentDiv class to be common amongst all the elements. This is because incremental class and id attributes are an anti-pattern and should be avoided.

Finally, this logic assumes that each button is appending different content from within its parent. If the content that's cloned is identical each time, clone a single element and remove the duplicates from the DOM, or use a <template /> to hold the content to be appended.

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