Completely cut and paste an element
Can I cut (completely, with all styles and attributes) an element from a location an开发者_JAVA技巧d paste it in another location (like body
) with jQuery?
Also, I want to place a string (or tag) in the old location, because I want to change its place to old location at the end of the script.
Can I? How?
appendTo() will automatically move the matched elements from their current location to the specified container, which seems to be what you want.
You can use after() to insert new content before moving the element:
$("#yourElement").after("<p>Element was there</p>").appendTo("body");
.detach()
is more appropriate for this task, as @lvan suggested.
jQuery(jQuery("#yourElement").detach()).appendTo("body");
you can do it by clone first copy it to another location
$('#yourElement').clone().appendTo('#anotherDiv');
then remove old one,
$('#parentOfOldElement #yourElement').remove();
or you can replace it to get it later
$('#parentOfOldElement #yourElement').replaceWith('<div id="toReplaceAgain">/div>');
insertAfter()
and insertBefore()
are exactly cut and paste methods.
unless or untill you are not cloning any element all operator will move the selected element from one location to other ex: appendChild , appendTo , insertAfter , insertBefore and so on
精彩评论