javascript - Reattach a removed event handler doesn't work -


I want to delete the element's click when I click (I am using off (<) Code> for this), and when the element is not clicked (click 'active') you want to add it again. This is not clicked to indicate, I am using a class called 'disabled'.

But when I remove, I can not reconnect it again. This only incident again!

This is what I am trying to do:

  $ ('.my-element'). ('Click', function () {$ ('.my-element'). Off ('click');}); Var disabled = setInterval (function () (if ($ ('.my-element'). Haclass ('no-click')) {$ ('my-element'). On ();}}, 1000) ;  

I am using Set Interval () to see whether the element has not been clicked. If it is not, then it can be used again ().

I also tried to remove the event handler in the browser console:

  $ ('my element') closed () .;  

But when I try to reconnect the event handler ...

  $ ('.my-element'). On ();  

This does not work, and will not repeat the behavior.

at and off without any parameters There is no effect, ideally you should name the event and it is a handler for those methods.

  var handler = function () {// ...} $ ('.my-element'). ('Click.namespace', handler); // ... $ ('my-element'). Off ('click.namespace', handler);  

If the handler should be called only once, you may also consider using the a method besides setIntevarl Using a bad idea, you should force the handler after adding the class. In this case, I recommend using Event Deployment Technology:

  $ ('# aStaticParent'). ('Click', '.my-element.not-clicked', function () {// handler is called only when element 'not clicked' classname $ (this) .removeClass ('not clicked went');});  

Comments