我非常生气 - 或许有人能帮助我解决这个问题。
我需要在 AJAX 调用后重新绑定链接的点击事件,但出于某种原因,它无法正常工作。
这是我的代码:
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').bind('click'); return false;
});
}
有什么想法吗?
感谢回复-我已经修改了代码,但问题仍然存在:
function makeActive() {
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').live('click', makeActive);
return false;
});
}
}
$('.active').live('click', makeActive);
.live(arg, fn)
被.on(bind, arg, fn)
取代了。我忽略了.on
的可选参数,它是用于 AJAX 调用的类或 ID 的位置。与在.live
中调用之前放置不同。另一篇文章强调.on
函数必须绑定到具有子调用的静态元素上。 - The Thirsty Ape$('.active').on('click', function () {
对我无效,但这个解决方案$(document).on('click', '.active', function () {
起作用了。 - ewroman