jquery temporary unbinding events

后端 未结 2 955
慢半拍i
慢半拍i 2020-12-31 08:47

maybe I\'m totally missing something about even handling in jQuery, but here\'s my problem.

Let\'s assume there are some event binding, like

$(elemen         


        
相关标签:
2条回答
  • 2020-12-31 09:26

    Good if the event is bound to multiple elements:

    $('.foo').click(function() {
        if ( ! $(this).hasClass('flag')) {
            do something
        }
    });
    

    (add class 'flag' to sort of unbind, add it to 'bind')

    0 讨论(0)
  • 2020-12-31 09:50

    Another way could be to use custom events, something along these lines:

    var flag = 0;
    $(element).bind("mousemove", function() {
        if(flag) {
            $(this).trigger("supermousemove");
        } else {
            $(this).trigger("magicmousemove");
        }
    }).bind("supermousemove", function() {
        // do something super
    }).bind("magicmousemove", function() {
        // do something magical
    }); 
    
    $("#foo").click(function() {
        flag = flag == 1 ? 0 : 1; // simple switch
    });
    

    Highly annoying demo here: http://jsfiddle.net/SkFvW/

    0 讨论(0)
提交回复
热议问题