To move a div to another parent using jQuery, you can use the appendTo() or insertAfter() methods. Here's an example:

HTML:

<div id="divToMove">I am a div</div>
<div id="parent1">
  <h2>Parent 1</h2>
</div>
<div id="parent2">
  <h2>Parent 2</h2>
</div>

JavaScript:

$(document).ready(function() {
  // Move divToMove to parent2
  $('#divToMove').appendTo('#parent2');
  
  // Alternative way to move divToMove to parent2
  // $('#divToMove').insertAfter('#parent2');
});

In the above example, the divToMove div is moved from parent1 to parent2 using the appendTo() method. Alternatively, you can use the insertAfter() method to achieve the same result.

Move or Cut Div to Another Parent with jQuery: AppendTo() and InsertAfter()

原文地址: https://www.cveoy.top/t/topic/qawQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录