简体   繁体   English

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

[英]jQuery - How to move some tag element with text inside to closest div

I have following HTML structure: 我有以下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>

Now I want to move <i>..</i> tag with value inside <div>..</div> tag like: 现在我想在<div>..</div>标签内移动<i>..</i>标签,如:

<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>

I have tried with jQuery like: 我试过像jQuery一样:

$('.green_box i').appendTo('.green_box div');

but not working. 但没有工作。 Any idea how to move <i>..</i> tag with closest <div>..</div> element. 任何想法如何移动<i>..</i>标签与最近的<div>..</div>元素。

My JSFiddle: https://jsfiddle.net/47htz1yb/ 我的JSFiddle: https ://jsfiddle.net/47htz1yb/

Thanks 谢谢

You can use each() method to do this work. 您可以使用each()方法执行此工作。

 $(".green_box").each(function(){ $(this).find(".icheckbox_flat-red_green").append($(this).find("i")); }); 
 .icheckbox_flat-red_green > i { color: red; } 
 <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> 

Using above CSS, every i element in icheckbox_flat-red_green class get red color. 使用上面的CSS, icheckbox_flat-red_green类中的每个i元素icheckbox_flat-red_green red

Try this: https://jsfiddle.net/47htz1yb/3/ 试试这个: https//jsfiddle.net/47htz1yb/3/

$('.green_box i').each(function(index, icon) {
    $(icon).siblings('div').append(icon)
})

Problem with $('.green_box i').appendTo('.green_box div'); 问题$('.green_box i').appendTo('.green_box div'); is that it will basically take all the i elements and put it in the first .green_box div . 是它将基本上采取所有i元素并将其放在第一个.green_box div

What you need is to cycle through all the i elements, find sibling div and append the i there. 你需要的是循环遍历所有i元素,找到兄弟div并将i追加到那里。

Try this: 试试这个:

$('.green_box i').each(function(){ 
    var $this = $(this),
        $prev = $this.prev(),
        $i = $this.remove();

    $prev.append($i);
});
$('.green_box i').each(function (i) {
    $('.green_box div').eq(i).append($(this));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM