简体   繁体   English

突变摘要库:如何将html元素附加到添加的元素

[英]Mutation Summary Library: How to append html elements to added elements

I'm using Mutation Summary library to observe when a particular type of elements have been added and then append my html element to each of them: 我正在使用Mutation Summary库来观察何时添加了特定类型的元素,然后将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'
    });
}

Code above is not working. 上面的代码不起作用。 I'm not sure I can even use appendChild on an object. 我不确定我什至可以在对象上使用appendChild Any help would be appreciated! 任何帮助,将不胜感激!

Your problem is mixing together "bare" DOM elements from Mutation Summary response and jQuery elements. 您的问题是将Mutation Summary响应和jQuery元素中的“裸” DOM元素混合在一起。 Pure DOM appendChild does not understand your jQuery-wrapped btn . 纯DOM appendChild无法理解您的jQuery包装的btn

So, you need to make them both to be the same type: 因此,您需要使它们都属于同一类型:

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

Either works, but the first one is probably more idiomatic. 两种方法都可以,但是第一个可能更惯用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM