jQuery - 如何将一些带有文本的标记元素移动到最近的div中

时间:2016-06-06 12:04:49

标签: javascript jquery html

我有以下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/

由于

4 个答案:

答案 0 :(得分:3)

您可以使用each()方法完成此项工作。

&#13;
&#13;
$(".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;
&#13;
&#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));
});