我在使用 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/