How to catch creation of DOM elements and manipulate them with jQuery

前端 未结 1 1151
情书的邮戳
情书的邮戳 2020-12-11 10:44

I\'m trying to devise a method of when adding a simple div element with a class and some data-* in it, it will replace it or add into it some other elements. This method sho

相关标签:
1条回答
  • 2020-12-11 11:31

    You could use a DOM Level 3 Event, like DOMNodeInserted. This could look like:

    $(document).bind('DOMNodeInserted', function(event) {
        // A new node was inserted into the DOM
        // event.target is a reference to the newly inserted node
    });
    

    As an alternative, you might checkout the .liveQueryhelp jQuery plugin.

    update

    In referrence to your comment, have a look at http://www.quirksmode.org/dom/events/index.html, only browser which do not support it are the Internet Explorers of this this world (I guess IE9 does at least).

    I can't say much about the performance, but it should perform fairly well.

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