简体   繁体   中英

Remove event handler function added by another function

I am currently writing a custom context menu class which utilizes jQuery largely.

We call a function to assign a context menu to a particular element within the DOM. The assign function appends the necessary markup to the DOM, and then adds in keyboard support by adding a handler function to the $(document).keydown() method.

As soon as someone clicks on the document itself, the context menu is removed from the DOM. If the user then clicks on the element again, we add the markup to the DOM again, and re-assign the handler function.

The problem that I am facing is that the event handlers are added each time the user clicks the element. Is there a simple way with jQuery to remove the handler assigned to $(document).keydown() by the assign function only ?

There are other functions within my script that also add listeners for the $(document).keydown() event, and I don't want to remove all those functions - only those added by the context menu assignment script. Is this possible with jQuery?

I think on and off , using events names / namespaces to isolate handlers, should work.

http://api.jquery.com/on/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM