【问题标题】:How can I get a tag type with a MutationObserver?如何使用 MutationObserver 获取标签类型?
【发布时间】:2019-11-14 12:42:49
【问题描述】:
        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 标签

【问题讨论】:

  • 如果您使用console.dir,则可以更轻松地查看可用的内容。无论其类型如何,它都会始终列出您正在记录的内容的属性

标签: javascript mutation-observers


【解决方案1】:

您可以使用nodeName

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 2017-09-01
    • 2019-07-09
    • 1970-01-01
    相关资源
    最近更新 更多