简体   繁体   English

删除动态生成 <div> 点击

[英]Remove dynamically generated <div> on click

I am trying to a <div> when it is clicked. 单击时我正在尝试<div> When I tried with .live() it shows me: 当我尝试使用.live()它会告诉我:

object has no method live() 对象没有方法live()

I am using jQuery version 1.9, so live has been removed. 我正在使用jQuery版本1.9,所以live已被删除。

$(document).ready(function(){
    $('#addhelper').click(function(){
        $('div#containerr').append('<div class ="helpcont"><input type="text" name="helper_caption[]" class="input-large" placeholder="Caption">'+
      '<input type="text" name="helper_url" class="input-large" placeholder="Url">'+
      '<input type="text" name = "helper_source" class="input-large" placeholder="Source"><button class = "remove" type="button">remove</button></div>');
    });

    $("button.remove").on({
        click: function(){
            $(this).remove('div.helpcont');
        }
    });
});
$("#containerr").on('click', '.remove', function(){
  $(this).closest('.helpcont').remove();
});

#containerr = the closest parent that is not added dynamically #containerr = 动态添加的最近父级

click = event (you can add multiple events by separating them with spaces) click = event(您可以通过用空格分隔来添加多个事件)

.remove = the selector on which to trigger the event .remove =触发事件的选择器


PS: Use selectors like #id instead of element#id . PS:使用#id选择器而不是element#id #id IDs should be unique anyway, so there's no need to do it the slow way, by making jQuery retrieve all DIV elements, and then searching for the one with the given ID. ID无论如何都应该是唯一的,所以不需要通过让jQuery检索所有DIV元素,然后搜索具有给定ID的那个元素来缓慢地进行。

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

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