jQuery绑定单击AJAX调用后的链接 [英] jQuery binding click to a link after AJAX call

查看:60
本文介绍了jQuery绑定单击AJAX调用后的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很生气-也许有人可以帮助我.

I'm getting furious - perhaps someone will be able to help me with this.

在AJAX调用后,我需要将点击重新绑定到链接,但是由于某种原因,它不起作用.

I need to re-bind the click to the link after AJAX call, but for some reason it doesn't want to work.

这是我的代码:

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

有什么主意吗?

感谢您的答复-我已经修改了代码,但问题仍然存在:

Thanks for the responses - I've amended the code, but the problem is still there:

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

推荐答案

如果要在 Ajax调用后执行success处理程序,则必须在success处理程序中添加重新绑定:

You would have to add the rebinding in the success handler if you want to execute it after the Ajax call:

success: function(data) {
    elem.replaceWith(data);
    $('.active').bind('click', /* some function needs to go here*/);
}

也就是说,在这种情况下, live() .on()] .如果您还有其他未被替换的.active链接,这也可以防止重复分配点击处理程序.

That said, in this case, live() or delegate() are probably better options [update: now that jQuery 1.7 is out, everything can be done with .on()]. This would also prevent double assignment of click handlers, in case you have other .active links that have not been replaced.

更新:关于更新的代码:live的使用方式无法达到目的.请阅读其文档.您正在做的是在单击链接时分配一个单击处理程序,这意味着您要一遍又一遍地添加单击处理程序.

Update: Regarding your updated code: The way you are using live defeats its purpose. Please read its documentation. What you are doing is assigning a click handler when the the link is clicked, which means that you are adding click handlers over and over again.

这是您代码的改进版本.

This is an improved version of your code.

$('.active').live('click', function(event) {
    var elem = $(this);
    var url = $(this).attr('href');
     $.ajax({
         url: url,
         dataType: 'html',
         success: function(data) {
              elem.replaceWith(data);                             
         }       
     });    
     event.preventDefault();
     event.stopPropagation();
});

这篇关于jQuery绑定单击AJAX调用后的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆