Best way to remove an event handler in jQuery?

前端 未结 20 2666
庸人自扰
庸人自扰 2020-11-21 11:38

I have an input type=\"image\". This acts like the cell notes in Microsoft Excel. If someone enters a number into the text box that this input-image

20条回答
  •  耶瑟儿~
    2020-11-21 11:40

    This wasn't available when this question was answered, but you can also use the live() method to enable/disable events.

    $('#myimage:not(.disabled)').live('click', myclickevent);
    
    $('#mydisablebutton').click( function () { $('#myimage').addClass('disabled'); });
    

    What will happen with this code is that when you click #mydisablebutton, it will add the class disabled to the #myimage element. This will make it so that the selector no longer matches the element and the event will not be fired until the 'disabled' class is removed making the .live() selector valid again.

    This has other benefits by adding styling based on that class as well.

提交回复
热议问题