function dom() { // 创建一个MutationObserver实例 let mutationObserver = new MutationObserver(function(mutationsList, observer) { for(var mutation of mutationsList) { console.log("用户:",mutation.target.lastChild.lastChild.childNodes[1].textContent,'信息:', mutation.target.lastChild.lastChild.lastChild.textContent); } }); // 选择要监视的DOM元素,这里假设是抖音Web端的弹幕容器 let targetElement = document.querySelector("#_douyin_live_scroll_container_ > div > div > .Iy_uuM55 > .__playerIsFullChatroom > .ro3xcipQ > .lF9JOwdS > div > .webcast-chatroom___messages > div > .webcast-chatroom___item-offset > div > div:nth-child(1)"); if(targetElement){ // 启动观察器,开始监视目标元素的变化 mutationObserver.observe(targetElement, { attributes: true, childList: true }); }else{ console.log('Target element not found'); } }
标签:js,抖音,targetElement,mutation,div,lastChild,弹幕 From: https://www.cnblogs.com/tcyweb/p/17836249.html