Javascript 如何将ajax中类中正在单击的元素作为目标
我想针对类中的某个div,我会使用$(这个),但这不起作用,因为我从另一个函数调用该类Javascript 如何将ajax中类中正在单击的元素作为目标,javascript,jquery,ajax,tree,parent,Javascript,Jquery,Ajax,Tree,Parent,我想针对类中的某个div,我会使用$(这个),但这不起作用,因为我从另一个函数调用该类 $(document).on('click', '.Player', function (e) { var id = $(this).find('.Song_Id').html(), that = $(this); $.ajax({ type: "POST", data: { data: id },
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html(),
that = $(this);
$.ajax({
type: "POST",
data: {
data: id
},
complete: function () {
that.attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
示例代码。
$(document).on('click', '.Player', function(e){
var id = $(this).find('.Song_Id').html();
$.ajax({
type:"POST",
data: {data:id},
complete: function(){
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url:"php/player/get_song.php"
}).done(function(f){
$('#Song_info').html(f)
});
})
从上面看,下面是我不知道如何实现的一行
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png'),
它假设目标是类“.player”,但不是整个类,而是单击的元素
谢谢 您可以将$(this)
存储在另一个变量中,并在函数中使用此变量
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html(),
that = $(this);
$.ajax({
type: "POST",
data: {
data: id
},
complete: function () {
that.attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
您可以将$(this)
存储在另一个变量中,并在函数中使用此变量
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html(),
that = $(this);
$.ajax({
type: "POST",
data: {
data: id
},
complete: function () {
that.attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
您可以将$(this)
存储在另一个变量中,并在函数中使用此变量
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html(),
that = $(this);
$.ajax({
type: "POST",
data: {
data: id
},
complete: function () {
that.attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
您可以将$(this)
存储在另一个变量中,并在函数中使用此变量
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html(),
that = $(this);
$.ajax({
type: "POST",
data: {
data: id
},
complete: function () {
that.attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
执行ajax回调时,默认情况下回调方法的执行上下文设置为ajax设置对象 可以使用的上下文选项传递自定义执行上下文
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html();
$.ajax({
type: "POST",
data: {
data: id
},
//use context to set the execution context of the callback
context: this,
complete: function () {
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
此对象将成为所有Ajax相关回调的上下文。通过
默认情况下,上下文是表示ajax设置的对象
在调用中使用($.ajaxSettings与传递给的设置合并
$.ajax)。例如,指定一个DOM元素作为上下文将
将其作为请求的完整回调的上下文
执行ajax回调时,默认情况下回调方法的执行上下文设置为ajax设置对象 可以使用的上下文选项传递自定义执行上下文
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html();
$.ajax({
type: "POST",
data: {
data: id
},
//use context to set the execution context of the callback
context: this,
complete: function () {
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
此对象将成为所有Ajax相关回调的上下文。通过
默认情况下,上下文是表示ajax设置的对象
在调用中使用($.ajaxSettings与传递给的设置合并
$.ajax)。例如,指定一个DOM元素作为上下文将
将其作为请求的完整回调的上下文
执行ajax回调时,默认情况下回调方法的执行上下文设置为ajax设置对象 可以使用的上下文选项传递自定义执行上下文
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html();
$.ajax({
type: "POST",
data: {
data: id
},
//use context to set the execution context of the callback
context: this,
complete: function () {
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
此对象将成为所有Ajax相关回调的上下文。通过
默认情况下,上下文是表示ajax设置的对象
在调用中使用($.ajaxSettings与传递给的设置合并
$.ajax)。例如,指定一个DOM元素作为上下文将
将其作为请求的完整回调的上下文
执行ajax回调时,默认情况下回调方法的执行上下文设置为ajax设置对象 可以使用的上下文选项传递自定义执行上下文
$(document).on('click', '.Player', function (e) {
var id = $(this).find('.Song_Id').html();
$.ajax({
type: "POST",
data: {
data: id
},
//use context to set the execution context of the callback
context: this,
complete: function () {
$(this).attr('src', '../images/appicons/2/16x16/refresh - Red.png')
},
url: "php/player/get_song.php"
}).done(function (f) {
$('#Song_info').html(f)
});
})
此对象将成为所有Ajax相关回调的上下文。通过
默认情况下,上下文是表示ajax设置的对象
在调用中使用($.ajaxSettings与传递给的设置合并
$.ajax)。例如,指定一个DOM元素作为上下文将
将其作为请求的完整回调的上下文