javascript - jQuery 帮助删除追加后的 div

标签 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>

测试 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;

});

关于javascript - jQuery 帮助删除追加后的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337096/

相关文章:

javascript - 在 MEAN.JS 应用程序中未使用 Angular $HTTP Get 读取 JSON 文件

php - Swiftmailer:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]

php - 如何使用jquery发送多个post请求?

javascript - 如何从外部函数查找 jQuery DropDownCheckList 所选项目

javascript - 使用 jquery 从输入中获取文件名列表

jquery - 如何拥有 jQuery 效果的多个实例

javascript - CORS/xhr.getRequestHeaders

javascript - 在页面加载时将网站翻译成任何特定语言

javascript - 从链接元素获取第一个属性

php - Codeigniter RESTful API 服务器