Javascript 删除、淡出和删除注释
删除注释时,行数不会更新为不包含实际注释数。 因为我使用fadeout函数而不是remove()函数,因为我希望它从视图中淡出 如何先淡出,然后删除 我尝试过这一点,但没有帮助:Javascript 删除、淡出和删除注释,javascript,jquery,fade,Javascript,Jquery,Fade,删除注释时,行数不会更新为不包含实际注释数。 因为我使用fadeout函数而不是remove()函数,因为我希望它从视图中淡出 如何先淡出,然后删除 我尝试过这一点,但没有帮助: $('#item_'+DbNumberID).fadeOut("slow", function() { $(this).remove(); }); 守则: 编辑: 这是我的全部代码。它能简单吗 //##### Send delete Ajax request to response.php #####
$('#item_'+DbNumberID).fadeOut("slow", function() {
$(this).remove();
});
守则:
编辑:
这是我的全部代码。它能简单吗
//##### Send delete Ajax request to response.php #########
$("body").on("click", "#comment .del_button", function(e) {
e.preventDefault();
//e.returnValue = false;
var clickedID = this.id.split('-'); //Split string (Split works as PHP explode)
var DbNumberID = clickedID[1]; //and get number from array
var myData = 'action=delete&id='+ DbNumberID; //build a post data structure
jQuery.ajax({
type: "POST", // HTTP method POST or GET
url: "add-comment.php", //Where to make Ajax calls
dataType: "text", // Data type, HTML, json etc.
data: myData, //Form variables
success: function(response){
//on success, hide element user wants to delete.
$('#item_'+DbNumberID).fadeOut(500, function() {
setTimeout(function() {
$('#item_'+DbNumberID).remove();
}, 500);
});
var rowCount = $('#comment li').length;
rowCount--;
alert(rowCount);
if (rowCount == 0) {
$("#comment").html('<div class="PageWarning">No comments.</div></div>');
}
return false;
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
});
//发送删除Ajax请求到response.php#########
$(“正文”)。在功能(e)中的(“单击”,“注释.删除”按钮){
e、 预防默认值();
//e、 returnValue=false;
var clickedID=this.id.split('-');//split string(split与PHP分解一样工作)
var dbnumberrid=单击edid[1];//并从数组中获取数字
var myData='action=delete&id='+dbnumberrid;//构建post数据结构
jQuery.ajax({
键入:“POST”,//HTTP方法POST或GET
url:“add comment.php”,//在哪里进行Ajax调用
数据类型:“text”,//数据类型、HTML、json等。
data:myData,//表单变量
成功:功能(响应){
//成功后,隐藏用户要删除的元素。
$('#item_'+dbnumberrid).fadeOut(500,function(){
setTimeout(函数(){
$('#item'+DbNumberID).remove();
}, 500);
});
var rowCount=$('#comment li')。长度;
行数--;
警报(行计数);
如果(rowCount==0){
$(“#注释”).html('无注释');
}
返回false;
},
错误:函数(xhr、ajaxOptions、thrownError){
//出现错误时,我们会提醒用户
警报(thrownError);
}
});
});
这有点老套,但您可以设置一个超时,其持续时间与淡出时间一样长
$('#item_'+DbNumberID).fadeOut(500, function() {
setTimeout(function() {
$('#item_'+DbNumberID).remove();
}, 500);
});
你也可以试试
$('#item_'+DbNumberID).fadeOut('slow').queue(function(next) {
$(this).remove();
next();
});
答案已被选中,但我仍将回答,以使此页面更有用:) 你的“成功”处理程序
你能创建一个JSFIDLE来说明吗?那么在
setTimeout
之后这也不起作用了?是时候发布你生成的HTML了:)是的。。。。。。。。。但是我的主代码能更简单些吗?是的,这很有效我这样做是为了显示,当它工作得很完美时,不会有更多的注释,因为您的代码很小。。。去罗马的千种方法,我更愿意这样做,比如发送整个列表作为请求的答案,并更新列表,使用json有很多简单的方法。但是如果你目前的状态运行得很好,没有任何缺点……使用它,但我不能保证,我甚至不知道为什么淡出(回调)不起作用
$('#item_'+DbNumberID).fadeOut('slow').queue(function(next) {
$(this).remove();
next();
});
success: function(response){
//on success, hide element user wants to delete.
$('#item_'+DbNumberID).fadeOut(500, function() {
setTimeout(function() {
$('#item_'+DbNumberID).remove();
}, 500);
});
var rowCount = $('#comment li').length;
rowCount--;
alert(rowCount);
if (rowCount == 0) {
$("#comment").html('<div class="PageWarning">No comments.</div></div>');
}
return false;
},
success: function(response){
var rowCount = $('#comment li').length;
//on success, hide element user wants to delete.
$('#item_'+DbNumberID).fadeOut(500, function() {
$('#item_'+DbNumberID).remove();
/* the code that has been moved into this callback */
rowCount = $('#comment li').length;
alert(rowCount);
if (rowCount == 0) {
$("#comment").html('<div class="PageWarning">No comments.</div></div>');
}
});
return false;
},