Listen to changes within a DIV and act accordingly

后端 未结 7 556
谎友^
谎友^ 2020-11-28 23:34

I have a function that grabs an XML document and transforms it according to an XSL document. It then places the result into a div with the id laneconfigdisplay

相关标签:
7条回答
  • 2020-11-29 00:15

    Though the event DOMSubtreeModified is deprecated, its working as of now, so for any makeshift projects you can use it as following.

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

    In the long term though, you'll have to use the MutationObserver API.

    0 讨论(0)
  • 2020-11-29 00:19

    You can opt to create your own custom events so you'll still have a clear separation of logic.

    Bind to a custom event:

    $('#laneconfigdisplay').bind('contentchanged', function() {
      // do something after the div content has changed
      alert('woo');
    });
    

    In your function that updates the div:

    // all logic for grabbing xml and updating the div here ..
    // and then send a message/event that we have updated the div
    $('#laneconfigdisplay').trigger('contentchanged'); // this will call the function above
    
    0 讨论(0)
  • 2020-11-29 00:19

    The change event is limited to input, textarea & and select.

    See http://api.jquery.com/change/ for more information.

    0 讨论(0)
  • 2020-11-29 00:24

    There is an excellent jquery plugin, LiveQuery, that does just this.

    Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated.

    For example you could use the following code to bind a click event to all A tags, even any A tags you might add via AJAX.

    $('a').livequery('click', function(event) { 
        alert('clicked'); 
        return false; 
    }); 
    

    Once you add new A tags to your document, Live Query will bind the click event and there is nothing else that needs to be called or done.

    Here is a working example of its magic...

    0 讨论(0)
  • 2020-11-29 00:26

    Try this

    $('#D25,#E37,#E31,#F37,#E16,#E40,#F16,#F40,#E41,#F41').bind('DOMNodeInserted DOMNodeRemoved',function(){
              // your code;
           });
    

    Do not use this. This may crash the page.

    $('mydiv').bind("DOMSubtreeModified",function(){
      alert('changed');
    });
    
    0 讨论(0)
  • 2020-11-29 00:33

    http://api.jquery.com/change/

    change does only work on input form elements.

    you could just trigger a function after your XML / XSL transformation or make a listener:

    var html = $('#laneconfigdisplay').html()
    setInterval(function(){ if($('#laneconfigdisplay').html() != html){ alert('woo'); html = $('#laneconfigdisplay').html() } }, 10000) //checks your content box all 10 seconds and triggers alert when content has changed...
    
    0 讨论(0)
提交回复
热议问题