开发者

jQuery - Wrap div to a specific element

开发者 https://www.devze.com 2023-01-28 08:36 出处:网络
<div id=\"last\"></div> <p id=\"text\">text</p> <a id=\"link\" href=\"xxx\">link</a>
<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 a <div></div> to the a element, and insert it after div element, the result should be:

<div id="last"></div&g开发者_开发技巧t;
<div><a id="link" href="xxx">link</a></div>
<p id="text">text</p>

Tried the code below, but not works

<!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>

Thanks you


Should look like:

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

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

Ref.: .wrap()


$('#link').wrap('<div></div>'); don't need to detach or insertAfter, just use wrap function


This should do the trick

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


In order to effectively do a Cut and Paste action on your element, you want to first wrap it in the DIV tags, detach it from the DOM and then insert it back in your target location (i.e. your "last" element):

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

精彩评论

暂无评论...
验证码 换一张
取 消