簡體   English   中英

使用Array的Javascript中的HTML表排序有效,但是為什么呢?

[英]HTML table sort in Javascript with Array works but why?

在此代碼中:

 let container = document.getElementById('variables'); Array.from(container.querySelectorAll('tr')) .sort((a, b) => a.getAttribute("name").localeCompare(b.getAttribute("name"), )) .forEach(tr => container.appendChild(tr)); 
 <table id="variables"> <tr name=B> <td>B</td> </tr> <tr name=A> <td>A</td> </tr> <tr name=C> <td>C</td> </tr> </table> 

我創建一個未排序的HTML表,並使用JavaScript對其進行排序。

我的作品,但我不明白為什么我不得到原始數組跟在后面的排序一,因為forEachappendChild到原始表

該代碼背后的魔力是什么?

foo.appendChild(bar)bar放在foo的末尾。

由於一個元素不能同時存在於兩個位置,因此,如果它已經是另一個元素的子元素,則首先將其從那里刪除。

即它與foo.appendChild(bar.cloneNode(true))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM