我有以下HTML标记:
<div class="form-item">
<i>item 1</i>
<div class="content">
some content for item 1
</div>
<div class="description">
some description for item 1
</div>
</div>
<div class="form-item">
<i>item 2</i>
<div class="content">
some content for item 2
</div>
<div class="description">
some description for item 2
</div>
</div>
<div class="form-item">
<i>item 3</i>
<div class="content">
some content for item 3
</div>
<div class="description">
some description for item 3
</div>
</div>
我希望在其父级中的相关.description
标记之后插入<i>
。问题是,每个.description
后插入每个<i>
。
我的尝试是:
$('.form-item .description').insertAfter('.form-item i');
但我只想在.description
i
怎么做?
由于
答案 0 :(得分:2)
你需要迭代它们中的每一个并附加到它们的兄弟i标签元素:
$('.form-item .description').each(function(){
$(this).insertAfter($(this).prevAll('i'));
});
<强> Working Demo 强>
答案 1 :(得分:0)
或者您可以使用$.fn.after
方法撤消任务:
$('.form-item i').after(function() {
return $(this).siblings('.description');
});
演示:
$('.form-item i').after(function() {
return $(this).siblings('.description');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-item">
<i>item 1</i>
<div class="content">
some content for item 1
</div>
<div class="description">
some description for item 1
</div>
</div>
<div class="form-item">
<i>item 2</i>
<div class="content">
some content for item 2
</div>
<div class="description">
some description for item 2
</div>
</div>
<div class="form-item">
<i>item 3</i>
<div class="content">
some content for item 3
</div>
<div class="description">
some description for item 3
</div>
</div>
&#13;