【发布时间】:2016-07-31 17:39:19
【问题描述】:
$('#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 无法识别新的ID。当我点击新按钮时,什么也没有发生,因为当新元素不存在时,JS 函数已经加载。
有没有办法让一些侦听器检查新 DIV 的负载,然后还加载与该元素 ID 对应的 JS 函数?
【问题讨论】:
-
stackoverflow.com/questions/8594408/… 也许这个链接可以提供帮助。
-
我花了一些时间了解如何使用 .delegate() 谢谢@Jo_bast 的提示。
标签: javascript jquery ajax dom