jQuery: Trying to run $.get within .each function
This code does get the index value of each element with the "profileName" class. It also does run a $.get using each index value as the url. But it fails when I try to make it put the ajax response into each element with the "details" class that is the child element of each "profilName" div. Is this doable?
$('.profileName').each(function(index) {
var urlVal = $(".showProfile", this).attr('profile');
$.get(urlVal, function开发者_开发知识库(data) {
$(".details", this).html(data);
}, "html");
});
It's because this
doesn't refer to what you want inside the $.get()
success
callback. You can fix it by storing it beforehand, like this:
$('.profileName').each(function(index) {
var urlVal = $(".showProfile", this).attr('profile'),
details = $(".details", this);
$.get(urlVal, function(data) {
details.html(data);
}, "html" );
});
$.get()
uses $.ajax()
underneath, and if a context
option isn't specified, the ajax object itself is this
in the callback.
精彩评论