How to add onload event to a div element

前端 未结 24 2275
谎友^
谎友^ 2020-11-22 00:26

How do you add an onload event to an element?

Can I use:

for t

24条回答
  •  醉酒成梦
    2020-11-22 00:49

    When you load some html from server and insert it into DOM tree you can use DOMSubtreeModified however it is deprecated - so you can use MutationObserver or just detect new content inside loadElement function directly so you will don't need to wait for DOM events

    var ignoreFirst=0;
    var observer = (new MutationObserver((m, ob)=>
    {
      if(ignoreFirst++>0) {
        console.log('Element add on', new Date());
      }
    }
    )).observe(content, {childList: true, subtree:true });
    
    
    // simulate element loading
    var tmp=1;
    function loadElement(name) {  
      setTimeout(()=>{
        console.log(`Element ${name} loaded`)
        content.innerHTML += `
    My name is ${name}
    `; },1500*tmp++) }; loadElement('Michael'); loadElement('Madonna'); loadElement('Shakira');

提交回复
热议问题