0

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

4

2 回答 2

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;

});
于 2013-10-15T07:46:06.617 回答
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;
});

希望能帮助到你....

于 2013-10-15T07:58:40.043 回答