jQuery:尝试在.each函数中运行$ .get

1 jquery

此代码确实使用"profileName"类获取每个元素的索引值.它还使用每个索引值作为url运行$ .get.但是当我尝试将ajax响应放入每个元素时,它会失败,而"details"类是每个"profilName"div的子元素.这可行吗?

$('.profileName').each(function(index) {
    var urlVal = $(".showProfile", this).attr('profile');
    $.get(urlVal, function(data) {
        $(".details", this).html(data);
    }, "html");
});
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 5

这是因为this在回调中没有提到你想要的东西.您可以通过预先存储来修复它,如下所示:$.get() success

$('.profileName').each(function(index) {
  var urlVal = $(".showProfile", this).attr('profile'),
      details = $(".details", this);
  $.get(urlVal, function(data) {
    details.html(data); 
  }, "html" ); 
});
Run Code Online (Sandbox Code Playgroud)

$.get()$.ajax()在下面使用,如果context没有指定选项,则ajax对象本身this在回调中.