How to wait until an element exists?

后端 未结 19 2238
广开言路
广开言路 2020-11-22 09:14

I\'m working on an Extension in Chrome, and I\'m wondering: what\'s the best way to find out when an element comes into existence? Using plain javascript, with an interval t

19条回答
  •  我在风中等你
    2020-11-22 09:45

    How about the insertionQuery library?

    insertionQuery uses CSS Animation callbacks attached to the selector(s) specified to run a callback when an element is created. This method allows callbacks to be run whenever an element is created, not just the first time.

    From github:

    Non-dom-event way to catch nodes showing up. And it uses selectors.

    It's not just for wider browser support, It can be better than DOMMutationObserver for certain things.

    Why?

    • Because DOM Events slow down the browser and insertionQuery doesn't
    • Because DOM Mutation Observer has less browser support than insertionQuery
    • Because with insertionQuery you can filter DOM changes using selectors without performance overhead!

    Widespread support!

    IE10+ and mostly anything else (including mobile)

提交回复
热议问题