如何使用MutationObserver获取标签类型?

时间:2019-07-03 19:15:10

标签: javascript mutation-observers

        let mObserver = new MutationObserver((mutationsList, observer) => {
            mutationsList.forEach((mutation) => {
                if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                    mutation.addedNodes.forEach((node) => {
                        console.log(node)
                    })
                }
            })
        })

        mObserver.observe(document.body, {
            childList: true,
            subtree: true
        })

我怎么知道添加的node的类型?我只想定位a标签

1 个答案:

答案 0 :(得分:2)

您可以使用nodeName

if (node.nodeName.toLowerCase() === 'a') {
    // Do things
}