jQuery帮助在追加后删除div

时间:2013-10-12 17:26:56

标签: javascript php jquery html css

我对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>

            TESTD         X    

2 个答案:

答案 0 :(得分:0)

用这个来拍摄:

$('.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;

});

答案 1 :(得分:0)

试试这个......

$('.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;
});

希望它有所帮助......