簡體   English   中英

刪除動態生成 <div> 點擊

[英]Remove dynamically generated <div> on click

單擊時我正在嘗試<div> 當我嘗試使用.live()它會告訴我:

對象沒有方法live()

我正在使用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 = 動態添加的最近父級

click = event(您可以通過用空格分隔來添加多個事件)

.remove =觸發事件的選擇器


PS:使用#id選擇器而不是element#id #id ID無論如何都應該是唯一的,所以不需要通過讓jQuery檢索所有DIV元素,然后搜索具有給定ID的那個元素來緩慢地進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM