简体   繁体   中英

listener if element was added to DOM

I am fully aware of MutationObserver but what I need is different:

I would like to listen to an element which has been created in the memory, and that specific element can be appended anywhere (thus I do not want to observe everything because it would be an overkill as things gets changed everywhere on the app, all the time).

The listener would fire when the specific element has been inserted to the DOM. just a way to follow a single element instead of watching the whole DOM. it's kinda backwards approach but I think it is the right way to go in my case.

How can this be done? thanks!

$myobject.appendTo('body').trigger('myobserver');

$myobject.on('myobserver',function(){
    //do stuff when myobject is added to DOM
});

You could use this CSS trick too: http://www.backalleycoder.com/2012/04/25/i-want-a-damnodeinserted/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM