How to handle undo/redo event in javascript?

前端 未结 6 1951
时光取名叫无心
时光取名叫无心 2020-12-10 08:42

I\'m trying to detect whenever a form input\'s value changes using Javascript & JQuery. Unfortunately, I find JQuery\'s $(elem).change() insufficient becaus

6条回答
  •  借酒劲吻你
    2020-12-10 09:33

    You can monitor all the changes using MutationObserver. This won't give you event for every keydown and keyup, but it kind of consolidate multiple changes and give it out to you as single event.

      var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
      var observer = new MutationObserver(function(mutations) {  
        mutations.forEach(function(mutation) {
            // mutation.target will give you element which has been modified.
            // mutation.addedNodes and mutation.removedNodes will give you operations that were performed on the node
            // happy coding :)
          });
      });
      observer.observe(elementsToMonitor, {
        attributes: true, 
        childList: true, 
        characterData: true 
       });
    

    More info about MutationObserver https://developer.mozilla.org/en/docs/Web/API/MutationObserver

提交回复
热议问题