简体   繁体   English

将元素移到父元素的末尾

[英]Move element to the end of parent element

I have the following HTML in my application: 我的应用程序中包含以下HTML:

    <div class="work-item">
        <div class="image-container">                
        </div>
        <div class="text-container">
        </div>
    </div>

    <div class="work-item">
        <div class="text-container">                
        </div>
        <div class="image-container">                
        </div>
    </div>

    <div class="work-item">
        <div class="image-container">                
        </div>
        <div class="text-container">
        </div>
    </div>

    <div class="work-item">
        <div class="text-container">                
        </div>
        <div class="image-container">                
        </div>
    </div>

I'd like to take any element that has class text-container , and if it's the first element under work-item , move it after the div with class image-container , so that every div element looks like 我想使用任何具有class text-container元素,如果它是work-item下的第一个元素,则将其移到div之后,并使用image-container类,以便每个div元素看起来像

    <div class="work-item">
        <div class="image-container">                
        </div>
        <div class="text-container">
        </div>
    </div>

I tried just removing it first with this: 我试着只是先删除它:

$(".work-item .text-container:first-child").remove();

And that worked fine, but when I tried to move it with this, it didn't work at all, nothing happened: 效果很好,但是当我尝试与此移动时,它根本不起作用,什么也没发生:

$("#work .work-item .text-container:first-child").insertAfter($(this).parent()["last-child"]);

Anyone know what I'm doing wrong here? 有人知道我在做什么错吗?

you can use this: 您可以使用此:

 $('.text-container').each(function(index, item) { var $this = $(item); $this.appendTo($this.parent()); }); 
 .work-item{ margin:20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="work-item"> <div class="image-container">img </div> <div class="text-container">txt </div> </div> <div class="work-item"> <div class="text-container">txt </div> <div class="image-container">img </div> </div> <div class="work-item"> <div class="image-container">img </div> <div class="text-container">txt </div> </div> <div class="work-item"> <div class="text-container">txt </div> <div class="image-container">img </div> </div> 

Try it this way, where you save the parent element as a variable 以这种方式尝试,将父元素保存为变量

var theParent = $(".work-item .text-container:first-child").parent();

$(".work-item .text-container:first-child").detach().appendTo(theParent);

and then use detach instead of remove, so that it still saves the info in memory, and then append it to the parent. 然后使用分离而不是删除,以便它仍将信息保存在内存中,然后将其附加到父项。

just use insertAfter can achieve your purpose. 只需使用insertAfter即可达到您的目的。
This is my JSFiddle. 这是我的JSFiddle。

var text = $('.text-container');
var img = $('.image-container');
text.insertAfter(img);

try 尝试

$(".work-item .text-container:nth-child(1)").each(function () {
    var elm = $(this);
    if (elm.next().is($(".image-container"))) {
         var parent = elm.parent();
         parent.append(elm.detach());
    }

});

DEMO DEMO

Because, The first element may be “.text-container” or ".image-container". 因为,第一个元素可能是“ .text-container”或“ .image-container”。

If the first element is ".image-container". 如果第一个元素是“ .image-container”。

$("#work .work-item .text-container:first-child").length == 0;

So you get the first element is not correct. 所以你得到的第一个元素是不正确的。

This is my: 这是我的:

 $(document).ready(function() { var workItems = $(".work-item"); var len = workItems.length,i = 0,el,first; for(;i < len; i++){ el = workItems[i]; first = $(el).children().first(); if (first.hasClass("text-container")){ $(el).append(first); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="work-item"> <div class="image-container">img </div> <div class="text-container">txt </div> </div> <div class="work-item"> <div class="text-container">txt </div> <div class="image-container">img </div> </div> <div class="work-item"> <div class="image-container">img </div> <div class="text-container">txt </div> </div> <div class="work-item"> <div class="text-container">txt </div> <div class="image-container">img </div> </div> 

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

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