【问题标题】:Removing event handler with .off使用 .off 删除事件处理程序
【发布时间】:2013-04-18 16:29:18
【问题描述】:

我无法使用 .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

【问题讨论】:

    标签: jquery events event-handling


    【解决方案1】:

    更正您的代码,尚未测试但应该可以工作

    $(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');
        };
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      相关资源
      最近更新 更多