简体   繁体   English

如何删除在 Javascript 中动态添加的元素

[英]How to remove an element that is added dynamically in Javascript

I have the following code to create some element:我有以下代码来创建一些元素:

<div id="parent">
    <div id="block_1" >
        <div>
          <input type="text">
        </div>
        <img src="arrow.jpg">
        <div>
          <input type="text">
        </div>
        <div><a href="#" class="remove_block">Remove</a></div>
    </div>
</div>

the result look like this:结果如下所示:

在此处输入图片说明

When user presses the ADD button it will go to a Javascript function and create the same block of div.当用户按下 ADD 按钮时,它将转到 Javascript 函数并创建相同的 div 块。 Here is the code:这是代码:

function add_block() {
var curDiv = $('#parent');
var i = $('#parent div').size()/4 + 1;
var newDiv='<div id="block_'+ i + '" class="parent_div">' +
'<div>' +
'<input type="text">' +
'</div>' +
'<img src="arrow.jpg">' +
'<div>' +
'<input type="text">' +
'</div><div><a class="remove_block" href="#">Remove</a></div>' +
'</div>';
$(newDiv).appendTo(curDiv);
};

Whenever the user press the "Remove" link on the left hand side of the block, that corresponding block should be removed.每当用户按下块左侧的“删除”链接时,应删除相应的块。 And this is what I did:这就是我所做的:

$('a.remove_block').on('click',function(events){
   $(this).parents('div').eq(1).remove();
});

The problem is that only the remove in the original block work, the rest didn't .问题是只有原始块中的 remove 起作用,其余的没有。 Anybody know why?有人知道为什么吗?

在此处输入图片说明

I am new to jQuery and Javascript, so I really appreciate any help and suggestion Note: I use jQuery 2.0.3我是 jQuery 和 Javascript 的新手,所以我非常感谢任何帮助和建议 注意:我使用 jQuery 2.0.3

Because it's dynamic content, you can't bind events like the static content, it will not bind to the elements because they don't appear at the time you bind.因为它是动态内容,你不能像静态内容那样绑定事件,它不会绑定到元素,因为它们在你绑定时没有出现。

So you should bind event like this:所以你应该像这样绑定事件:

$('#parent').on('click', 'a.remove_block', function(events){
   $(this).parents('div').eq(1).remove();
});

You need to use event delegation for dynamically added elements.您需要对动态添加的元素使用事件委托。 Even though you have used .on() the syntax used does not use event delegation.即使您使用了.on()所使用的语法也不使用事件委托。

When you register a normal event it adds the handler to only those elements which exists in the dom at that point of time, but when uses event delegation the handler is registered to a element which exists at the time of execution and the passed selector is evaluated when the event is bubbled upto the element当您注册普通事件时,它仅将处理程序添加到当时存在于 dom 中的那些元素,但是当使用事件委托时,处理程序将注册到执行时存在的元素,并评估传递的选择器当事件冒泡到元素时

$(document).on('click', '.remove_block', function(events){
   $(this).parents('div').eq(1).remove();
});
$('a.remove_block').on('click',function(events){
  $(this).parents('.parent_div').remove();
  return false;
});

I had the situation where the dynamic element to remove wasn't a descendant of my event listener:我遇到过要删除的动态元素不是我的事件侦听器的后代的情况:

siteSel.on('select2:select', function (e) {
      // remove some other dynamic element on page.
});

Solution I found was using when() method.我发现的解决方案是使用when()方法。

siteSel.on('select2:select', function (e) {
      let dynamicElement = $('.element');
      $.when(dynamicElement).then(dynamicElement.remove());
});

You can use the .live for this:您可以为此使用 .live:

$('body').live('click','#idDinamicElement', function(){
   // your code here
});

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

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