$('#followUser').click(function() {
var userId = $(this).attr('data-userId'),
action = $(this).attr('data-action'),
currentUser = $(this).attr('data-currentUserId'),
elem = $(this);
$.ajax({
method: 'POST',
url: "/sci-profile/profile/follow",
data: {
userId: currentUser,
profileUserId: userId,
action: action
},
success: function(response)
{
elem.parent().load(window.location.href + ' #unFollowUser');
}
});
});
負荷要素點擊按鈕,加載JS新的div其所有的內容,這是很好的,但是,當加載新的div問題此刻開始後。刷新JS功能時用AJAX
JS不認識新的ID
。而當我點擊新按鈕時,什麼都不會發生,因爲當新元素不存在時JS函數已經加載。
有沒有辦法讓一些監聽器檢查新的DIV
的負載,然後加載對應於該元素的JS功能ID
?
[Event delegation](http://stackoverflow.com/questions/1687296/what-is-dom-event-delegation?answertab=votes#tab-top) – adeneo
http://stackoverflow.com/questions/8594408/reload-javascript-file-after-an-ajax-request也許這個鏈接可以提供幫助。 –
我花了一些時間瞭解如何使用.delegate()謝謝@Jo_bast提示。 –