用jQuery包装锚标签的图像标签

时间:2015-09-03 14:18:59

标签: jquery

我的Html结构如下:

<div class="content">
<img alt="" src="a.png" height="400" width="400">
<a href="https:www.google.com" class="avatar" ></a>
</div>
<div class="content">
<img alt="" src="b.png" height="400" width="400">
<a href="https:www.yahoo.com" class="avatar" ></a>
</div>

我想通过jquery将锚标记包装在图像标记周围,所以我的最终结果将是:

<div class="content">
<a href="https:www.google.com" class="avatar" >
<img alt="" src="a.png" height="400" width="400">
</a>
</div>
<div class="content">
<a href="https:www.yahoo.com" class="avatar" >
<img alt="" src="b.png" height="400" width="400">
</a>
</div>

使用jQuery执行此操作的最简单方法是什么?

谢谢,

4 个答案:

答案 0 :(得分:1)

您可以抓取图像并将其附加到锚标记,如下所示:

jQuery('img').each(function() {
    $(this).appendTo($(this).next('a'));
});

Fiddle

答案 1 :(得分:1)

请考虑以下事项:

$('.content img').each(function() {
    $(this).next('a').wrapInner($(this));
});

DEMO

$('.content img').each(function() {
   $(this).next('a').html($(this).clone());
   $(this).remove();
});

DEMO

答案 2 :(得分:0)

试试这个:

Insert Into Server1.Databasename.dbo.TableName(Col1,Col2,Col3)
Select Col1, Col2, Col3
From Server2.Databasename.dbo.TableName

答案 3 :(得分:-1)

您需要使用.wrap()功能。它非常快速和简单

$("content img").each(function(el, i){
   var a = $(this).next().clone();
   $(this).next().remove();
   $(this).wrap(a);
});

http://api.jquery.com/wrap/

相关问题