简体   繁体   English

用另一个替换html元素

[英]Replace html element with another

This Meteor template helper gets its raw html from mongodb collection, it then needs to replace a specific tr element with a pre created tr element myTr . 这个Meteor模板助手从mongodb集合获取其原始html,然后需要用预先创建的tr元素myTr替换特定的tr元素。 It would be nice to use javascript but the below is using jQuery which is not doing the job. 使用javascript会很好,但是下面使用的是jQuery,它无法完成任务。
How can it be done? 如何做呢? Thanks 谢谢

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
      }
    });
  }
});

Well, you have beatifull mix of variables. 好吧,您可以将变量完美地混合在一起。 Look

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

try changing jObj(this.parentElement) to $(this.parentElement) and it will work 尝试将jObj(this.parentElement)更改为$(this.parentElement) ,它将起作用

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

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