jQuery Event : Detect changes to the html/text of a div

后端 未结 13 2524
旧巷少年郎
旧巷少年郎 2020-11-22 06:15

I have a div which has its content changing all the time , be it ajax requests, jquery functions, blur etc etc.

Is there a way

13条回答
  •  不知归路
    2020-11-22 07:14

    Adding some content to a div, whether through jQuery or via de DOM-API directly, defaults to the .appendChild() function. What you can do is to override the .appendChild() function of the current object and implement an observer in it. Now having overridden our .appendChild() function, we need to borrow that function from an other object to be able to append the content. Therefor we call the .appendChild() of an other div to finally append the content. Ofcourse, this counts also for the .removeChild().

    var obj = document.getElementById("mydiv");
        obj.appendChild = function(node) {
            alert("changed!");
    
            // call the .appendChild() function of some other div
            // and pass the current (this) to let the function affect it.
            document.createElement("div").appendChild.call(this, node);
            }
        };
    

    Here you can find a naïf example. You can extend it by yourself I guess. http://jsfiddle.net/RKLmA/31/

    By the way: this shows JavaScript complies the OpenClosed priciple. :)

提交回复
热议问题