text

link Suppose I have the code ab...">

JQuery - Wrap div for a specific element

<div id="last"></div> <p id="text">text</p> <a id="link" href="xxx">link</a> 

Suppose I have the code above, how can I wrap the <div></div> element in an a element and insert it after the div element, the result should be:

 <div id="last"></div> <div><a id="link" href="xxx">link</a></div> <p id="text">text</p> 

Tried the code below but not working

 <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.4.4.js"></script> </head> <body> <div id="last"></div> <p id="text">text</p> <a id="link" href="xxx">link</a> <script> $('#link').wrap('<div></div>').detach().insertAfter('#last'); </script> </body> </html> 

thank you

+4
source share
4 answers

It should look like:

 $('#link').wrap('<div></div>').parent().insertAfter('#last'); 

Example: http://www.jsfiddle.net/4yUqL/2/

Ref . : . wrap ()

+3
source

$('#link').wrap('<div></div>'); No need to disconnect or insert. Just use the transfer function

+3
source

That should do the trick

 $("#link").wrap("<div></div>").detach().insertAfter("#last") 
0
source

To effectively perform the β€œCut” and β€œPaste” actions for your element, you want to first transfer it to the DIV tags, disconnect it from the DOM and then paste it back to the target location (ie your β€œlast” element)

 $('#link').wrap('<div></div>').detach().insertAfter('#last'); 
0
source

All Articles