最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome Extension: fire an event when element created? - Stack Overflow

programmeradmin4浏览0评论

I'd like to fire an event when an element is added to the document. I've read the JQuery documentation for on() and the list of events but none of the events seem to concern element creation.

I must monitor the DOM as I do not control when the element is added to the document (as my Javascript is a Chrome Extension content script)

I'd like to fire an event when an element is added to the document. I've read the JQuery documentation for on() and the list of events but none of the events seem to concern element creation.

I must monitor the DOM as I do not control when the element is added to the document (as my Javascript is a Chrome Extension content script)

Share Improve this question edited Nov 4, 2013 at 11:22 mikemaccana asked Dec 19, 2011 at 11:44 mikemaccanamikemaccana 123k110 gold badges428 silver badges531 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

I know this is an old question, that already has an answer, but since things have changed, I thought I'd add an updated answer for people landing on this page looking for an answer.

The DOM Mutation Events have been deprecated. According to MDN (regarding DOM Mutation Events):

Deprecated
This feature has been removed from the Web. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

One should use the new MutationObserver API, which is also more efficient.
(The mutation-summary library now provides a useful inteface to this new API.)

Example usage:

// Create an observer instance.
var observer = new MutationObserver(function (mutations) {
  mutations.forEach(function (mutation) {
    console.log(mutation.type);
  });
});

// Config info for the observer.
var config = {
  childList: true, 
  subtree: true
};

// Observe the body (and its descendants) for `childList` changes.
observer.observe(document.body, config);

...

// Stop the observer, when it is not required any more.
observer.disconnect();

As mentioned, I am not creating my own elements, so firing off an event when I create the element isn't an option.

The best solution to this is the Mutation Observer API, for which the Mutation Summary provides a friendly interface.

For the simplest option, you might want to examine LiveQuery which is effectively the 'DOM listener' that you're after. It should be used with caution however, as it has the potential to be quite heavyweight, performance-wise.

If you're preferring to 'roll your own' - with .on() it should work for all current and future elements - but the added elements would need to match the selector. For example, if you wire an event up to all classes of .myClass and you then injected a new element of the same class to the DOM, the event should fire. Effectively, the .on() API rolls-up bind/live/delegate from prior versions of jQuery, the latter two of which work against current and future DOM elements.

As a plement of @SpaceBison's answer, you can create your own events when you create these elements. For example:

function create_element() {
    // Create an element
    $( 'body' ).append( "<p>Testing..</p>" )
    // Trigger
    $( 'body' ).trigger( 'elementCreated' );
}
function monitor_elements() {
    $("body").on("elementCreated", function(event){
    alert('One more');
    });
}

$(document).ready(function(){

     monitor_elements();

     for (var i=0; i < 3; i++) {
         create_element();
     }

});

Live example: http://jsfiddle/9NsAh/

But of course, it's only usefull when you create your own elements.

发布评论

评论列表(0)

  1. 暂无评论