Detect element content changes with jQuery

后端 未结 15 2522
余生分开走
余生分开走 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: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) */});
    }
    

提交回复
热议问题