Detect paragraph element change with JQuery

后端 未结 7 1948
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 17:58

Is it possible to detect if the content of a paragraph has been changed in JQuery ?

I tried the below code.

Text

7条回答
  •  既然无缘
    2020-12-06 18:16

    The approved answer didn't work for me so here is my revised and working example for anyone looking.

    function getNode(){
    //Store the test paragraph node
    var test = $('#test');
    
    if(!test){
      // The node we need may not exist yet
      // Wait 500ms and try again
    window.setTimeout(getNode, 500);
    return;
    }
    //Function to change the paragraph
    var changeParagraph = function () {
        var d = new Date();
        var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
        test.text(time);
    };
    
    //Bind the paragraph changing event
    $('#submit1').on('click', changeParagraph);
    
    //Observe the paragraph
    this.observer = new MutationObserver(changeParagraph);
    this.observer.observe(test, {characterData: true, childList: true});
    });
    //Delay calling our function until page loads
    setTimeout(function() {
        getNode();
    }, 5000);
    

提交回复
热议问题