簡體   English   中英

通過 javascript 將特定項目移動到 div 元素的底部

[英]Move a specific item to the bottom of a div element through javascript

例如,我在下面的 div 中動態生成了 lis 中的四個不同值,並且數據來自 Rest API:

<div id=make_last>
  <li>one</li>
  <li>two</li>
  <li>three</li>
  <li>four</li>
</div>

我想通過 javascript 或 jquery 將<li>two</li>移動到底部如下圖

<div>
  <li>one</li>
  <li>three</li>
  <li>four</li>
  <li>two</li>
</div>

有什么想法我怎么能做到這一點?

使用each找到它

var elemTwo;

$("li").each(function() {
  if (this.innerText == "two") elemTwo = this;
}); 

將某些內容移動到 DOM 元素末尾的最簡單方法是 append 它,因為appendChild將元素放在末尾; 它會自動從當前的 position 中刪除該項目。

const container = document.querySelector('#make_last'); // or however you get a reference
const itemToMove = container.children[1]; // or however you want to find the child
container.appendChild(itemToMove);

暫無
暫無

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

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