用.off删除事件处理程序 [英] Removing event handler with .off

查看:81
本文介绍了用.off删除事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



点击一个框后,我需要为该框删除事件处理程序,目前我已经挂钩.clickable,并删除该类,并尝试删除该事件处理程序然后将事件处理程序应用到具有.box类的所有框。

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

if(!$('。box')。(':animated' ){

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

$这个)
.clone()// clone元素
.addClass('cloned')//添加类到克隆元素
.css({
'position':'absolute '
,'top':position.top
,'left':position.left
})
.appendTo('body'); //附加到文档

animate(position,targetPosition);

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

};

});

感谢大家的建议和帮助,如果有人想看到最终的演示这里是一个小提琴



工作版本

解决方案

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



$ $ $ $ $ $ $($)$($)$($)$($) (':animated')){
var position = $(this).position();
var targetPosition = $('。target')。position();
$ )
.clone()// clone元素
.addClass('cloned')//将类添加到克隆元素
.css({
'position':'absolute'
,'top':position.top
,'left':position.left
})
.appendTo('body'); //附加到文档
animate(position,targetPosition);
$(this)
.removeClass('clickable')
.addClass('notActive')
.off('click');
};
});


I am unable to remove the event handler using .off for the boxes.

Once a box is clicked I need the event handler removed for that box, currently I have made the hook ".clickable" and remove the class and tried to remove the event handler then apply the event handler back to all boxes with a class of ".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');

        };

    });

Thanks everyone for your advice and help, if anyone would like to see the final demo "working" here is a fiddle

working version

解决方案

Correction for your code, haven't tested but should work

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

这篇关于用.off删除事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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