Detect element content changes with jQuery

后端 未结 15 2525
余生分开走
余生分开走 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 05:09

    You can add a callback option to html (,or any) function:

    $.fn.oldHtml = $.fn.html;
    $.fn.html = function(html,fn){
      fn = fn || function(){};
      var result =  this.oldHtml(html);
      fn();
      return result;
    };
    $('body').html(11,function(){alert("haha");});
    

    Demo here.

    You do the change on some element, not the element is forced to change by something that you have to catch.

提交回复
热议问题