我有以下HTML结构:
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
</div>
<i class="">1</i>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
</div>
<i class="">2</i>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
</div>
<i class="">3</i>
</span>
现在,我想在<i>..</i>
标记内移动<div>..</div>
标记,如:
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
<i class="">1</i>
</div>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
<i class="">2</i>
</div>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green" style="position: relative;">
<input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
<ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
<i class="">3</i>
</div>
</span>
我尝试过使用jQuery:
$('.green_box i').appendTo('.green_box div');
但没有工作。任何想法如何移动<i>..</i>
标记与最近的<div>..</div>
元素。
我的JSFiddle:https://jsfiddle.net/47htz1yb/
由于
答案 0 :(得分:3)
您可以使用each()
方法完成此项工作。
$(".green_box").each(function(){
$(this).find(".icheckbox_flat-red_green").append($(this).find("i"));
});
&#13;
.icheckbox_flat-red_green > i {
color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="green_box">
<div class="icheckbox_flat-red_green">
<input type="radio" name="minimal-radio">
<ins class="iCheck-helper"></ins>
</div>
<i class="">1</i>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green">
<input type="radio" name="minimal-radio">
<ins class="iCheck-helper"></ins>
</div>
<i class="">2</i>
</span>
<span class="green_box">
<div class="icheckbox_flat-red_green">
<input type="radio" name="minimal-radio">
<ins class="iCheck-helper"></ins>
</div>
<i class="">3</i>
</span>
&#13;
使用上面的CSS,i
类中的每个icheckbox_flat-red_green
元素都会获得red
颜色。
答案 1 :(得分:1)
试试这个:https://jsfiddle.net/47htz1yb/3/
$('.green_box i').each(function(index, icon) {
$(icon).siblings('div').append(icon)
})
$('.green_box i').appendTo('.green_box div');
的问题在于它基本上会占用所有i
元素并将其放在第一个.green_box div
中。
您需要循环浏览所有i
元素,找到同级div
并在i
附加{。}}。
答案 2 :(得分:1)
试试这个:
$('.green_box i').each(function(){
var $this = $(this),
$prev = $this.prev(),
$i = $this.remove();
$prev.append($i);
});
答案 3 :(得分:0)
$('.green_box i').each(function (i) {
$('.green_box div').eq(i).append($(this));
});