簡體   English   中英

jQuery幫助在追加后刪除div

[英]jQuery help remove div after append

我的jQuery有問題,創建后不會刪除div ...這是代碼

$('.del').on('click', function() {
        //delItem = $(this);
        var data_id = $(this).attr('rel');
        $.post('index/xhrDelete', {'data_id': data_id}, function(o) {
            //delItem.parent().remove(); // i have tried this too
            $(this).parent().remove();
        }, 'json');
        return false;
});

它刪除了div,但是當我手動刷新時...但是我希望不刷新頁面,這里是html ....

<div>
     ccc
     <a class="del" href="#" rel="5">X</a>
</div>
<div>
test
     <a class="del" href="#" rel="21">X</a>

經過測試的X

試一下:

$('.del').on('click', function(e) { // Notice the 'e' in the function
    $this = $(e.target); // Use it to retrieve the element that fired the event and turn it into a jQuery object
    var data_id = $(this).attr('rel');
    $.post('index/xhrDelete', {'data_id': data_id}, function(o) {
        $this.parent().remove(); // You just need to remove its parent
    }, 'json');

    return false;

});

試試看...

$('.del').on('click', function() {
       var self=this;
        var data_id = $(self).attr('rel');
        $.post('index/xhrDelete', {'data_id': data_id}, function(o) {
            //delItem.parent().remove(); // i have tried this too
            $(self).parent().remove();
        }, 'json');
        return false;
});

希望能幫助到你....

暫無
暫無

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

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