Detect changes in the DOM

后端 未结 7 2316
温柔的废话
温柔的废话 2020-11-21 05:45

I want to execute a function when some div or input are added to the html. Is this possible?

For example, a text input is added, then the function should be called.<

7条回答
  •  有刺的猬
    2020-11-21 06:06

    How about extending a jquery for this?

       (function () {
            var ev = new $.Event('remove'),
                orig = $.fn.remove;
            var evap = new $.Event('append'),
               origap = $.fn.append;
            $.fn.remove = function () {
                $(this).trigger(ev);
                return orig.apply(this, arguments);
            }
            $.fn.append = function () {
                $(this).trigger(evap);
                return origap.apply(this, arguments);
            }
        })();
        $(document).on('append', function (e) { /*write your logic here*/ });
        $(document).on('remove', function (e) { /*write your logic here*/ ) });
    

    Jquery 1.9+ has built support for this(I have heard not tested).

提交回复
热议问题