用另一个替换html元素

时间:2016-08-18 18:35:47

标签: javascript jquery meteor

此Meteor模板助手从mongodb集合中获取其原始html,然后需要使用预先创建的tr元素tr替换特定的myTr元素。使用javascript会很好,但下面是使用jQuery而不是这项工作 怎么做到呢?谢谢

Template.myHelper.helpers({
  info: function() {
    let myTr = document.createElement('tr');
    //do things in myTr.
    let data = myCollection.findOne({});
    let jObj = $($.parseHTML(data.rawHTML));
    jObj.find('td.myClass').each(function() {
      if (this.textContent === "found it") {
        this.parentElement.id = 'myId';
        jObj(this.parentElement).replaceWith(myTr); //<-- failed to repalce
      }
    });
  }
});

1 个答案:

答案 0 :(得分:0)

好吧,你有美丽的变量组合。看

  

jObj(this.parentElement).replaceWith(myTr); //<-- failed to repalce

jObj //that's jquery element
this //that's html element
this.parentElement //that's also html element

jObj(this.parentElement) //what's this? supposed to be jquery object

尝试将jObj(this.parentElement)更改为$(this.parentElement),它会正常工作