Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用变异观测器查找新出现的DOM类?_Javascript_Html_Mutation Observers - Fatal编程技术网

Javascript 使用变异观测器查找新出现的DOM类?

Javascript 使用变异观测器查找新出现的DOM类?,javascript,html,mutation-observers,Javascript,Html,Mutation Observers,我想检测在facebook的新帖子出现在用户的订阅源上之后,当他们滚动时,会出现什么样的条目,我能够检测到新的dom更改,但我被困在了回调中 $(this)不等于新出现的DOM,我不知道该怎么办 // Necessary for Facebooks "neverending" scrolling var observer = new MutationObserver(function (mutations, observer) { // fired when a mutation occ

我想检测在facebook的新帖子出现在用户的订阅源上之后,当他们滚动时,会出现什么样的条目,我能够检测到新的dom更改,但我被困在了回调中

$(this)
不等于新出现的DOM,我不知道该怎么办

// Necessary for Facebooks "neverending" scrolling
var observer = new MutationObserver(function (mutations, observer) {
    // fired when a mutation occurs

    var x = $(this);
    console.log(x) // I'm stuck here, what to do?
});

// pass in the target node, as well as the observer options
observer.observe($('[id^=topnews_main_stream_]').get(0), {
    attributes: true,
    childList: true,
    characterData: true,
    subtree: true
});

您可以使用
突变
对象过滤突变的
类型
,其
addedNodes
属性将返回新添加的节点

var observer = new MutationObserver(function (mutations, observer) {
    mutations.forEach(function(mutation) {
        if (mutation.type == "childList") {
            console.log(mutation.addedNodes)
        }
    }); 
});
var MutationObserver=window.MutationObserver | | window.webkit MutationObserver | window.MozMutationObserver;
var list=document.querySelector(“#form”);
var观察者=新的突变观察者(功能(突变){
突变。forEach(功能(突变){
if(mutation.type==='childList'){
console.log(突变.添加节点)
}
});
});
观察者,观察者{
属性:正确,
儿童名单:是的,
characterData:true
});
setTimeout(函数(){
var数据=“”;
jQuery(“#表单”).append(数据)
},5000)

您应该在
回调
函数中记录变量
突变

var observer = new MutationObserver(function (mutations, observer) {
    mutations.forEach(function(mutation) {
        console.log(mutation);
    }); 
});

在MutationObserver callback
中,此
观察者
对象本身。这是正确的,我也发现了,但在facebook中,无法检测链接、照片或状态在哪里。DOM没有任何不同的迹象。叹气。你的代码工作正常,但根据facebook.com上的DOM,无法检测帖子是图像、链接还是状态。