Detect element content changes with jQuery

后端 未结 15 2272
余生分开走
余生分开走 2020-11-22 04:19

change() function works and detects changes on form elements, but is there a way of detecting when a DOM element\'s content was changed?

This does not w

相关标签:
15条回答
  • 2020-11-22 04:56

    Not possible, I believe ie has a content changed event but it is certainly not x-browser

    Should I say not possible without some nasty interval chugging away in the background!

    0 讨论(0)
  • 2020-11-22 04:57

    I know this post is a year old, but I'd like to provide a different solution approach to those who have a similar issue:

    1. The jQuery change event is used only on user input fields because if anything else is manipulated (e.g., a div), that manipulation is coming from code. So, find where the manipulation occurs, and then add whatever you need to there.

    2. But if that's not possible for any reason (you're using a complicated plugin or can't find any "callback" possibilities) then the jQuery approach I'd suggest is:

      a. For simple DOM manipulation, use jQuery chaining and traversing, $("#content").html('something').end().find(whatever)....

      b. If you'd like to do something else, employ jQuery's bind with custom event and triggerHandler

      $("#content").html('something').triggerHandler('customAction');
      
      $('#content').unbind().bind('customAction', function(event, data) {
         //Custom-action
      });
      

    Here's a link to jQuery trigger handler: http://api.jquery.com/triggerHandler/

    0 讨论(0)
  • 2020-11-22 04:58

    It's not strictly a jQuery answer - but useful to mention for debugging.

    In Firebug you can right-click on an element in the DOM tree and set up 'Break on Attribute Change':

    Element right-click in Firebug with Break on Attribute Change highlighted

    When an attribute is changed in a script, the debug window will appear and you can track down what it going on. There is also an option for element insertion and element removal below (unhelpfully obscured by the popup in the screengrab).

    0 讨论(0)
  • 2020-11-22 04:59

    Often a simple and effective way to achieve this is to keep track of when and where you are modifying the DOM.

    You can do this by creating one central function that is always responsible for modifying the DOM. You then do whatever cleanup you need on the modified element from within this function.

    In a recent application, I didn't need immediate action so I used a callback for the handly load() function, to add a class to any modified elements and then updated all modified elements every few seconds with a setInterval timer.

    $($location).load("my URL", "", $location.addClass("dommodified"));
    

    Then you can handle it however you want - e.g.

    setInterval("handlemodifiedstuff();", 3000); 
    function handlemodifiedstuff()
    {
        $(".dommodified").each(function(){/* Do stuff with $(this) */});
    }
    
    0 讨论(0)
  • 2020-11-22 05:02

    I'm developing tiny JS library called mutabor (https://github.com/eskat0n/mutabor) which intended to simplify usage of DOM Mutation Events. See demo.html for examples.

    0 讨论(0)
  • 2020-11-22 05:05

    These are mutation events.

    I have not used mutation event APIs in jQuery, but a cursory search led me to this project on GitHub. I am unaware of the project's maturity.

    0 讨论(0)
提交回复
热议问题