简体   繁体   English

jQuery的多个追加仅追加最后一个

[英]JQuery Multiple Append Only Appends Last one

I want to use jQuery to create a table. 我想使用jQuery创建表。 Currently I have an empty table body and I would like to use some jQuery to fill up the table: 当前我有一个空的表主体,我想使用一些jQuery来填充表:

 var $tr = $("<tr>"), $td = $("<td>"); var date = '2018-01-01' $td.text(date); $tr.append($td); $td.text("New Years"); $tr.append($td); $("#body").append($tr); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody id='body'> </tbody> <table> 

But this only appends the second td . 但这仅附加了第二个td The first one gets overwritten. 第一个被覆盖。 Any ideas on how to fix? 有关如何解决的任何想法?

Like Taplar said in comments, if the element already is in DOM, the .append will only "move it". 就像Taplar在评论中说的那样,如果元素已经在DOM中,则.append只会“移动它”。

Now in your example, you append it a the same place, but also change the text. 现在,在您的示例中,将其添加到相同的位置,同时还要更改文本。

Try the .clone() method to duplicate elements. 尝试使用.clone()方法来复制元素。

 var $tr = $("<tr>"); var $td = $("<td>"); var date = '2018-01-01'; $td.text(date); $tr.append($td); var secondCell = $td.clone().text("New Years"); $tr.append(secondCell); $("#body").append($tr); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody id='body'> </tbody> </table> 

 var $tr1 = $("<tr>"), $tr2 = $("<tr>"), $tr3 = $("<tr>"), $td = $("<td>"); var date = '2018-01-01'; $td.text(date); //various ways to accomplish it $tr1.append($td.prop('outerHTML')); $tr2.append($td.clone()); $tr3.append($('<td>', { text: date })); $td.text("New Years"); $tr1.append($td.prop('outerHTML')); $tr2.append($td.clone()); $tr3.append($('<td>', { text: 'New Years' })); $("#body").append([$tr1, $tr2, $tr3]); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody id='body'> </tbody> <table> 

You have to clone $td, so that DOM treats it as a new element. 您必须克隆$ td,以便DOM将其视为新元素。

 var $tr = $("<tr>"), $td = $("<td>"); var date = '2018-01-01' $td.text(date); $tr.append($td.clone()); $td.text("New Years"); $tr.append($td.clone()); $("#body").append($tr); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody id='body'> </tbody> <table> 

You can try next code without jquery: 您可以尝试不使用jquery的下一个代码:

let tbody =  document.querySelector('#body');
let tr =  document.createElement('tr');
let td = document.createElement('tr');
td.textContent = '2018-01-01';;
let tdClone = td.cloneNode();
tdClone.textContent = "New Years";
tr.append(td,tdClone);
tbody.append(tr);

I think that is true way, because you can create new td instead of clone created td (maybe for example this td has special classes or atributes), and you code repaint DOM only after insert tr in table. 我认为这是正确的方式,因为您可以创建新的td而不是克隆创建的td(例如,此td具有特殊的类或属性),并且仅在将tr插入表中后才对DOM进行编码。

Hmm... Maybe this could be an alternative to do that: 嗯...也许这可以替代:

var texts = ["2018-01-01", "New Years"];

$.each(texts, function(i, val){
    $("<tr><td>"+val+"</td></tr>").appendTo("#body")
});

$.each gets each item on the array texts , wrap it on tr and td , then append to the result on #body . $.each获取数组texts每个项目,将其包装在trtd ,然后追加到#body的结果上。

working fiddle 工作小提琴

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

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