0

我使用Mutation Summary library当元素的特定类型已被添加到观察,然后我的HTML元素附加到他们每个人:突变总结库:如何HTML元素附加到添加的元素

var observer = new MutationSummary({ 
     callback: theNextPageLoaded, 
     queries: [{ 
      element: "li.sweet" 
     }] 
}); 

function theNextPageLoaded(summaries) { 
    var sc = summaries[0], 
    sc_length = sc.added.length, 
    btn = $('<button>', { 
      class: 'my_btn', 
      text: 'Please work!' 
     }); 

    sc.added.forEach(function(newEl) { 
     newEl.appendChild(btn); 
     console.log(typeof(newEl)); //newEl's type is 'object' 
    }); 
} 

代码以上不起作用。我不确定我甚至可以在物体上使用appendChild。任何帮助,将不胜感激!

回答

1

您的问题是从一起Mutation Summary response和jQuery元素“裸” DOM元素混合。纯DOM appendChild不理解你的jQuery包装btn

所以,你需要让他们两个是同一种类型:

$(newEl).append(btn); // jQuery 
newEl.appendChild(btn.get(0)); // pure DOM 

无论是工作,但第一个可能是更地道。

+0

TIL ..谢谢Xan – kyw