开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜