How to tell when a dynamically created element has rendered

前端 未结 7 1563
我在风中等你
我在风中等你 2020-12-02 12:54

I need to accurately measure the dimensions of text within my web app, which I am achieving by creating an element (with relevant CSS classes), setting its innerHTML

7条回答
  •  时光取名叫无心
    2020-12-02 13:32

    According to @Elliot B.'s answer, I made a plan that suits me.

    const callback = () => {
      const el = document.querySelector('#a');
      if (el) {
        observer.disconnect();
        el.addEventListener('click', () => {});
      }
    };
    
    const observer = new MutationObserver(callback);
    observer.observe(document.body, { subtree: true, childList: true });
    

提交回复
热议问题