简体   繁体   English

使用jQuery append()删除附加的div

[英]Removing div's appended using jQuery append()

Hi i have a button which add's a div to my html using jquery append(). 嗨,我有一个按钮,它使用jquery append()将div添加到我的html中。 Now each of the div's appended has a close button which removes the appended div using jquery remove(). 现在,每个附加的div都有一个关闭按钮,该按钮使用jquery remove()删除附加的div。 The code looks like this: 代码如下:

$(document).ready(function(){
  $("#image-btn").click(function(){
  var $imageElement = $("<div class='image_element' id='image-element'><div class='image_holder' align='center'><input type='image' src='{{URL::asset('images/close-icon.png')}}' name='closeStory' class='closebtn' id='close-img-btn' width='22px'><button type='button' class='img_btn' id='img-btn'>Upload Image</button></div></div>");
  $("#element-holder").append($imageElement);
  $imageElement.fadeIn(1000);

  $("#close-img-btn").click(function(){
  $imageElement.remove();
  });
  });
});

Now the problem is arises while removing the appended div's. 现在,在删除附加的div时出现了问题。 When i add multiple div's (Each has a close button) and click on the close button of the last appended div nothing happens. 当我添加多个div(每个都有一个关闭按钮)并单击最后一个附加div的关闭按钮时,没有任何反应。 But clicking on the close button of the first appended div closes all the other appended div. 但是单击第一个附加div的关闭按钮将关闭所有其他附加div。

I am trying to close only the div that i hit close and not the others. 我正在尝试仅关闭我关闭的div,而不关闭其他。 How do i do this ? 我该怎么做呢 ? Would really appreciate if someone could guide me through. 如果有人可以指导我,我将不胜感激。 Thanks. 谢谢。

ID of an element must be unique, when you use id selector it will return only the first element with the id, so all the click handlers are added to the first button. 元素的ID必须唯一,当您使用ID选择器时,它将仅返回具有ID的第一个元素,因此所有单击处理程序都将添加到第一个按钮。

Use classes and event delegation 使用类和事件委托

$(document).ready(function () {
    $("#image-btn").click(function () {
        var $imageElement = $("<div class='image_element' ><div class='image_holder' align='center'><input type='image' src='{{URL::asset('images/close-icon.png')}}' name='closeStory' class='closebtn' width='22px'><button type='button' class='img_btn' >Upload Image</button></div></div>");
        $("#element-holder").append($imageElement);
        $imageElement.fadeIn(1000);
    });

    $("#element-holder").on('click', '.closebtn', function(){
        $(this).closest('.image_element').remove();
    })
});

Demo: Fiddle 演示: 小提琴

More Read: 阅读更多:

You need remove the parent/child near at button you press. 您需要在按下的按钮附近移除父母/子女。

For example: 例如:

 $("#close-img-btn").click(function(){
   $(this).parent('content-div').remove();
});

use $(this) instead of $imageElement 使用$(this)代替$ imageElement

 $(document).ready(function(){
      $("#image-btn").click(function(){
      var $imageElement = $("<div class='image_element' id='image-element'><div class='image_holder' align='center'><input type='image' src='{{URL::asset('images/close-icon.png')}}' name='closeStory' class='closebtn' id='close-img-btn' width='22px'><button type='button' class='img_btn' id='img-btn'>Upload Image</button></div></div>");
      $("#element-holder").append($imageElement);
      $imageElement.fadeIn(1000);

      $("#close-img-btn").click(function(){
      $(this).remove();
      });
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM