Javascript 使用jQuery执行脚本后的淡出div
有了这个函数,我想将数据(res_id)提交给脚本,并通过它的id(使用变量pos)淡出div,脚本被执行,但div不会淡出。为什么呢Javascript 使用jQuery执行脚本后的淡出div,javascript,jquery,Javascript,Jquery,有了这个函数,我想将数据(res_id)提交给脚本,并通过它的id(使用变量pos)淡出div,脚本被执行,但div不会淡出。为什么呢 $('.remove_resort').click(function() { pos = $(this).attr("id"); rem_res(); }); function rem_res() { $.get("/snowreport/request/remove.php", { res_id: pos },
$('.remove_resort').click(function() {
pos = $(this).attr("id");
rem_res();
});
function rem_res() {
$.get("/snowreport/request/remove.php", {
res_id: pos
}, function(data1) {
pos.fadeOut("slow");
});
}
pos
超出了范围。您应该将其作为参数传递给函数
$('.remove_resort').click(function() {
pos = $(this).attr("id");
rem_res(pos);
});
function rem_res(pos) {
$.get("/snowreport/request/remove.php", {
res_id: pos
}, function(data1) {
pos.fadeOut("slow");
});
}
您正在存储
pos=$(this).attr(“id”)
,它是一个字符串,而不是jQuery对象。您的原始代码所做的是尝试调用“somestring”.fadeOut(),这将不起作用。如果您为元素保留对jQuery对象的引用并调用fadeOut,它应该淡出
让我们重写这段代码,使它能够完成您所追求的,更加清晰和明显:
$('.remove_resort').click(function() {
rem_res(this);
});
function rem_res(element) {
var $element = $(element),
id = element.id;
$.get("/snowreport/request/remove.php", { res_id: id }, function(data) {
$element.fadeOut("slow");
});
}
pos
包含ID字符串,因此您需要$(“#”+pos)。淡出(“慢”)
但请稍候。。。他通过调用$(this.attr(“id”)
来设置pos。如果他只是将pos设置为$(this)
,那么他就不必重新创建新的jQuery对象。它还可以处理任何没有ID的对象。通过将元素传递给他的函数,他还可以避免pos
成为全局范围的变量。