jQuery-将div包装到特定元素 [英] jQuery - Wrap div to a specific element

查看:73
本文介绍了jQuery-将div包装到特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

假设我有上面的代码,如何将<div></div>包装到a元素上,然后将其插入div元素之后,结果应该是:

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

谢谢

推荐答案

应类似于:

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

示例: http://www.jsfiddle.net/4yUqL/2/

参考: .wrap()

这篇关于jQuery-将div包装到特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆