Jquery 确认甜蜜警报后更改点击元素的颜色
当我点击一个特定的类时,会弹出一个甜蜜的警报。如果我确认,它应该更改我单击的元素的背景色,并更改现有元素的背景色。如果我在点击后立即这样做,我可以用“this”这样的词: 但是因为我想在sweet警报之后再做,所以我不能使用它,也不知道如何获得clicked类Jquery 确认甜蜜警报后更改点击元素的颜色,jquery,Jquery,当我点击一个特定的类时,会弹出一个甜蜜的警报。如果我确认,它应该更改我单击的元素的背景色,并更改现有元素的背景色。如果我在点击后立即这样做,我可以用“this”这样的词: 但是因为我想在sweet警报之后再做,所以我不能使用它,也不知道如何获得clicked类 $( ".thumbs" ).on("click", ".thumb-wrapper", function() { var prodId = $( ".featured-wrapper" ).data("prodid");
$( ".thumbs" ).on("click", ".thumb-wrapper", function() {
var prodId = $( ".featured-wrapper" ).data("prodid");
var prodName = $(this).data("name");
swal({
title: 'Set Featured image',
text: "Are you sure you want to change it?",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, change it!'
}).then(function () {
swal(
'Changed!',
'Featured image changed.',
'success'
)
$.ajax({
url: 'functions/changed-featured-img.php',
type: 'POST',
data: {
prodId: prodId,
prodName: prodName
},
})
.done(function (data) {
$( ".featured-wrapper" ).css("background-color", "white");
// need to change the clicked class background to red.
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.log(textStatus + ': ' + errorThrown);
console.warn(jqXHR.responseText);
});
})
.catch(swal.noop)
});
使用body而不是“.thumb”
$(“document.body”)。在(“click”、“.thumb wrapper”上,function(){}使用body而不是“.thumb”
$(“document.body”)。在(“click”、“.thumb wrapper”上,function(){}将单击的元素保存到变量中(在调用
swal()
之前):
在以后的函数中,请使用:
$this.css("background-color", "red");
将单击的元素保存到变量(在调用
swal()
之前):
在以后的函数中,请使用:
$this.css("background-color", "red");
添加变量tis=$(此变量)
最后使用
tis.css("background-color", "white");
在ajax成功函数中添加变量tis=$(this)
最后使用
tis.css("background-color", "white");
在ajax成功函数中,我不是专家,但使用最接近的div而不是整个函数不是更好吗?是的,你是对的。为了获得最佳性能,你可以使用document.body我不是专家,但使用最接近的div而不是整个函数不是更好吗?是的,你是对的。为了获得最佳性能,你可以使用document.body
tis.css("background-color", "white");