Jquery 在显示删除警报之前更改行的颜色
单击删除按钮时,我想先更改行的颜色,然后显示删除确认警报。我试着这样做:Jquery 在显示删除警报之前更改行的颜色,jquery,Jquery,单击删除按钮时,我想先更改行的颜色,然后显示删除确认警报。我试着这样做: /** * Removes the selected row. */ $('.link-remove').on('click', function() { var parents = $(this).closest('tr'); parents.css('background-color', '#ff6666'); //this doesn't wor
/**
* Removes the selected row.
*/
$('.link-remove').on('click', function() {
var parents = $(this).closest('tr');
parents.css('background-color', '#ff6666'); //this doesn't work
var result = confirm("Are you sure you want to delete?");
if (result) {
//do something on delete
}
return false;
});
但这根本不会改变颜色
当我尝试在成功删除时更改行的背景色时,它会起作用:
$.post(
ajaxurl,
data,
function( response )
{
//alert(response);
console.log(response);
console.log("Success");
parents.css('background-color', '#ff6666'); //this works
}
);
但我想在确认显示之前而不是删除之后突出显示行
我怎样才能达到这个效果?是否可以在没有设置超时的情况下实现此效果
编辑:
HTML:
Settimeout是异步的,并被放入javascript事件循环中。一旦所有代码都被执行,它将在最后被调用。因此,
if(result)
将不可用
/**
* Removes the selected row.
*/
$('.link-remove').on('click', function() {
var parents = $(this).closest('tr');
parents.css('background-color', '#ff6666');
var result;
var secondsDelay = 2; //2 seconds
setTimeout(function() {
if(confirm("Are you sure you want to delete?")) {
// Do your code here
}
}, secondsDelay * 1000);
return false;
});
在html和工作代码段中使用
setTimeout
。为此我添加了表。如果我错了,请评论
$('.link remove')。在('click',function()上{
var parents=$(this).closest('tr');
parents.css('background-color','#ff6666');//这是有效的:)
setTimeout(函数(){
var result=confirm(“您确定要删除吗?”);
}, 500);
});代码>
显示您的HTML?您的代码应该可以工作,我想可能是控制台出错了check@BhushanKawadkar我检查了,没有控制台错误。更新的html代码中tr,table在哪里?如果结果是异步的,它应该更改背景颜色,对吗?是的,它应该更改颜色。我的问题是它没有更改颜色。即使我删除了setTimeout
函数以毫不延迟地显示删除确认警报,它也不会更改父级的背景颜色。如果没有看到实际的dom,我们无法签出
/**
* Removes the selected row.
*/
$('.link-remove').on('click', function() {
var parents = $(this).closest('tr');
parents.css('background-color', '#ff6666');
var result;
var secondsDelay = 2; //2 seconds
setTimeout(function() {
if(confirm("Are you sure you want to delete?")) {
// Do your code here
}
}, secondsDelay * 1000);
return false;
});