此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
}
});
}
});
答案 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)
,它会正常工作