Confused About MutationObserver

后端 未结 3 1667
遇见更好的自我
遇见更好的自我 2021-02-09 06:46

So I have been rattling my brain about using the MutationObserver and I haven\'t made any progress. I\'ve read about it on the W3C site and in the MDN. When reading it in the MD

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-09 07:09

    first you definitely should not use jQ selectors as they are not Node elements. second, you must be sure that query selector returns not null. To make sure try for the first time observer on document.body: it is definitely a Node object. Something like

    (
        new MutationObserver(function(mutationEventList){
            console.log(mutationEventList)
        })
    )
    .observe(document.body, {
        childList: true,
        attributes: true,
        characterData: true
    })
    

    When you will get familiar with targeting an observer and understand MutationObserverInit options values(they are described not as good as it could) you will be able to work with mutationObserver right.

提交回复
热议问题