简体   繁体   中英

Remove event from newly created element

How can I remove event to a newly created element, let's say I have no control to that dynamic element because it is generated from a plugin and there's no way for me to remove an event during plugin initialization.

So here's the case, I wanted to remove the event called touchmove in jQuery("#dynamic_element") element using .off("touchmove") on page load.

jQuery("#dynamic_element").off("touchmove")

Is there any jQuery method that I could use to poll if the dynamic element added in DOM is ready for manipulation?

I plan to mainly target iOS devices.

Your thoughts would be greatly appreciated.

Solution using MutationObserver (IE11, FF, CH, SA iOS 7)

var mo = new MutationObserver(function(e){
   e.forEach(function(record) {
       var addedNodes = Array.prototype.slice.call(record.addedNodes);           
       var dynamicElements = addedNodes.filter(function(e) { 
           return e.id && e.id === "dynamic_element";
       });

      if (dynamicElements.length) {
         jQuery(dynamicElements[0]).off("touchmove");
      }
   });
});

mo.observe(document.documentElement, { subtree: true,  childList: true });

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