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

后端 未结 13 2279
旧巷少年郎
旧巷少年郎 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 06:55

    If you don't want use timer and check innerHTML you can try this event

    $('mydiv').bind('DOMSubtreeModified', function(){
      console.log('changed');
    });
    

    More details and browser support datas are Here.

    Attention: in newer jQuery versions bind() is deprecated, so you should use on() instead:

    $('body').on('DOMSubtreeModified', 'mydiv', function(){
      console.log('changed');
    });
    
    0 讨论(0)
  • 2020-11-22 06:55

    Tried some of answers given above but those fires event twice. Here is working solution if you may need the same.

    $('mydiv').one('DOMSubtreeModified', function(){
        console.log('changed');
    });
    
    0 讨论(0)
  • 2020-11-22 06:56

    Since $("#selector").bind() is deprecated, you should use:

    $("body").on('DOMSubtreeModified', "#selector", function() {
        // code here
    });
    
    0 讨论(0)
  • 2020-11-22 06:58

    Following code works for me.

    $("body").on('DOMSubtreeModified', "mydiv", function() {
        alert('changed');
    });
    

    Hope it will help someone :)

    0 讨论(0)
  • 2020-11-22 06:58

    Use MutationObserver as seen in this snippet provided by Mozilla, and adapted from this blog post

    Alternatively, you can use the JQuery example seen in this link

    Chrome 18+, Firefox 14+, IE 11+, Safari 6+

    // Select the node that will be observed for mutations
    var targetNode = document.getElementById('some-id');
    
    // Options for the observer (which mutations to observe)
    var config = { attributes: true, childList: true };
    
    // Callback function to execute when mutations are observed
    var callback = function(mutationsList) {
        for(var mutation of mutationsList) {
            if (mutation.type == 'childList') {
                console.log('A child node has been added or removed.');
            }
            else if (mutation.type == 'attributes') {
                console.log('The ' + mutation.attributeName + ' attribute was modified.');
            }
        }
    };
    
    // Create an observer instance linked to the callback function
    var observer = new MutationObserver(callback);
    
    // Start observing the target node for configured mutations
    observer.observe(targetNode, config);
    
    // Later, you can stop observing
    observer.disconnect();
    
    0 讨论(0)
  • 2020-11-22 07:02

    There is no inbuilt solution to this problem, this is a problem with your design and coding pattern.

    You can use publisher/subscriber pattern. For this you can use jQuery custom events or your own event mechanism.

    First,

    function changeHtml(selector, html) {
        var elem = $(selector);
        jQuery.event.trigger('htmlchanging', { elements: elem, content: { current: elem.html(), pending: html} });
        elem.html(html);
        jQuery.event.trigger('htmlchanged', { elements: elem, content: html });
    }
    

    Now you can subscribe divhtmlchanging/divhtmlchanged events as follow,

    $(document).bind('htmlchanging', function (e, data) {
        //your before changing html, logic goes here
    });
    
    $(document).bind('htmlchanged', function (e, data) {
        //your after changed html, logic goes here
    });
    

    Now, you have to change your div content changes through this changeHtml() function. So, you can monitor or can do necessary changes accordingly because bind callback data argument containing the information.

    You have to change your div's html like this;

    changeHtml('#mydiv', '<p>test content</p>');
    

    And also, you can use this for any html element(s) except input element. Anyway you can modify this to use with any element(s).

    0 讨论(0)
提交回复
热议问题