jQuery事件处理程序不适用于附加元素 [英] jQuery event handlers not working on appended elements

查看:64
本文介绍了jQuery事件处理程序不适用于附加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送ajax请求,并且已将结果附加到div上.在此附加之后,如果我想单击附加的链接(exec jquery click函数),为什么单击函数不起作用? (对不起,英语不好:P)

I'm sending ajax request and i have appended the result on a div. After this append if i want to click on a link appended (exec jquery click function), why click function doesen't work? (Sorry for bad english :P )

jQuery('.more').live("click",function() {
    var ID = jQuery(this).attr("id");
    if(ID) {
        jQuery("#more"+ID).html('<img src="template/css/images/moreajax.gif" />');
    jQuery.ajax({
        type: "POST",
        url: "loadmore.php",
        data: "lastid="+ ID, 
        cache: false,
        success: function(html){
            $("#contentWall").append(html);
        jQuery("#more"+ID).remove(); // removing old more button
        }
    });
    } else {
        jQuery(".morebox").html('The End');// no results
    }
return false;
});

推荐答案

您需要使用 .live() .on() ,具体取决于您的jQuery版本.

You need to use .live() or .on(), depending on your jQuery version for this to work.

常规.click()仅适用于DOM中当前存在的元素,不适用于将来的添加.

The regular .click() only applies to the elements that are currently in the DOM, not for future additions.

这篇关于jQuery事件处理程序不适用于附加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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