删除和添加后面的元素后,JQuery点击功能不起作用 [英] JQuery click function not working after removing and adding back elements

查看:150
本文介绍了删除和添加后面的元素后,JQuery点击功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的点击功能

$ $ $ $''cal table tbody td')。on('click',function (){
if($(this).hasClass('available'))
{
alert('asd');
}
});

我遇到的问题是,在切换到下一个或上一个月后,我的点击功能在日历中不起作用。



例如,在我的JSFIDDLE中,如果你移动到前一个月,然后回到当前月份并执行点击功能,编辑:我使用了一个名为date.js的外部库,请查看我的jsfiddle以了解更新的内容。

/ p>

编辑2:更新jsfiddle链接

jsfiddle


解决方案

使用这个


$ ($(this).hasClass)b $ b

  $(document).on('click','。cal table tbody td',function(){
('available')){
alert('asd');
}
});

而非此

 ($(this).hasClass('available')){
)('click',function(){
if alert('asd');
}
});

前者正确替代委托


this is my click function

$('.cal table tbody td').on('click', function () {
    if($(this).hasClass('available'))
    {
        alert('asd');
    }
});

the problem i am having is that after i have switched to the next or previous month, my clicking function on the calendar does not work.

For example in my JSFIDDLE, if u move to the previous month and then move back to the current month and do the click function, it wouldn't work anymore.

EDIT: I'm using an external library called date.js, check out my jsfiddle for a clearer idea of what is going on.

EDIT 2: updated jsfiddle link

jsfiddle

解决方案

Use this

$(document).on('click','.cal table tbody td', function () {
        if ($(this).hasClass('available')) {
            alert('asd');
        }
});

instead of this

$('.cal table tbody td').on('click', function () {
        if ($(this).hasClass('available')) {
            alert('asd');
        }
    });

Former is the correct replacement for delegate

这篇关于删除和添加后面的元素后,JQuery点击功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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