2010-11-10 153 views
1

此代碼確實獲得了每個具有「profileName」類的元素的索引值。它也使用每個索引值作爲url運行$ .get。但是,當我嘗試使用每個「profilName」div的子元素「details」類將ajax響應放入每個元素時,它都會失敗。這是可行的嗎?

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

回答

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"); 
}); 

$.get()使用$.ajax()下,和if a context option isn't specified, the ajax object itself is this in the callback

+0

尼克,非常感謝!你救了我的工作;) – 2010-11-10 19:16:29