使用.off删除事件处理程序

时间:2013-04-18 16:29:19

标签: jquery events event-handling

我无法使用.off删除事件处理程序。

一旦单击一个框,我需要为该框删除事件处理程序,目前我已将钩子设为“.clickable”并删除该类并尝试删除事件处理程序,然后将事件处理程序应用回所有框中“.box”类。

        $('.clickable').off('click','**').on("click", function(event){

        if( !$('.box').is(':animated') ) {

            var position = $(this).position()
            ,   targetPosition =  $('.target').position();

            $(this)
                .clone() //clone element
                .addClass('cloned') //add class to cloned element
                .css({
                    'position' : 'absolute'
                ,   'top' : position.top 
                ,   'left' : position.left 
                })
                .appendTo('body'); //append to the document

                animate(position,targetPosition);

                $(this)
                    .removeClass('clickable')
                    .addClass('notActive');

        };

    });

感谢大家的建议和帮助,如果有人希望看到最终演示“工作”在这里是一个小提琴

working version

1 个答案:

答案 0 :(得分:3)

对您的代码进行更正,尚未经过测试但应该正常工作

$(document).on("click", '.clickable', function(event){
    if( !$('.box').is(':animated') ) {
        var position = $(this).position();
        var targetPosition =  $('.target').position();
        $(this)
            .clone() //clone element
            .addClass('cloned') //add class to cloned element
            .css({
                'position' : 'absolute'
            ,   'top' : position.top 
            ,   'left' : position.left 
            })
            .appendTo('body'); //append to the document
            animate(position,targetPosition);
            $(this)
                .removeClass('clickable')
                .addClass('notActive')
                .off('click');
    };
}); 
相关问题