Cut and Move a Div to Another Parent with jQuery: Solution and AJAX Integration
To move a div to another parent and remove it from the original position using jQuery, you can use the '.appendTo()' method along with the '.remove()' method. Here's an example:
// Move the div with id 'myDiv' to another parent
$('#myDiv').appendTo('#newParent');
// Remove the div from the last position
$('#oldParent #myDiv').remove();
If you are using this code after an AJAX request and the '.appendTo()' method is not working, make sure that you are executing the code after the AJAX request is complete. You can use the '.done()' method to ensure that the code runs only when the AJAX request is finished. Here's an example:
$.ajax({
// AJAX settings
})
.done(function(response) {
// Move the div with id 'myDiv' to another parent
$('#myDiv').appendTo('#newParent');
// Remove the div from the last position
$('#oldParent #myDiv').remove();
});
Make sure to replace 'myDiv', 'newParent', and 'oldParent' with the actual IDs or selectors of your elements.
原文地址: https://www.cveoy.top/t/topic/qaxb 著作权归作者所有。请勿转载和采集!