请帮帮我,我需要在DOM结构中替换(切换)标签。我有这个:
<div class="wrap">
<ul>
<li>
<div class="block">
<div class="title">title 1</div>
<div class="image">image 1 for title 1</div>
</div>
</li>
<li>
<div class="block">
<div class="title">title 2</div>
<div class="image">image 2 for title 2</div>
</div>
</li>
<li>
<div class="block">
<div class="title">title 3</div>
<div class="image">image 3 for title 3</div>
</div>
</li>
<li>
<div class="block">
<div class="title">title 4</div>
<div class="image">image 4 for title 4</div>
</div>
</li>
</ul>
</div>
我在结果中需要这个,我怎么得到它?
<div class="wrap">
<ul>
<li>
<div class="block">
<div class="image">image 1 for title 1</div>
<div class="title">title 1</div>
</div>
</li>
<li>
<div class="block">
<div class="image">image 2 for title 2</div>
<div class="title">title 2</div>
</div>
</li>
<li>
<div class="block">
<div class="image">image 3 for title 3</div>
<div class="title">title 3</div>
</div>
</li>
<li>
<div class="block">
<div class="image">image 4 for title 4</div>
<div class="title">title 4</div>
</div>
</li>
</ul>
</div>
我正在尝试使用jQuery .each()
,但结果我得到了像这样的结构
title 1
image 4
title 2
image 4
title 3
image 4
答案 0 :(得分:1)
您可以使用 insertBefore()
$.each($('.wrap .image'), function() {
$(this).insertBefore($(this).prev());
});
顺便说一下,您的HTML标记无效,您应该使用title
正确关闭与</div>
类的div,并在演示中为您修复了它。
<强> Fiddle Demo 强>
答案 1 :(得分:0)