jQuery: appendTo parent

不打扰是莪最后的温柔 提交于 2019-12-03 23:37:00

The following should suffice:

$("div>img").each(function(){
    $(this).appendTo($(this).parent());
});

See it working here: http://jsfiddle.net/EtxqL/

You can't infer each item's parent from the 'selector' parameter to appendTo(). The only way to do what you want is to loop through the items, appending each one to its parent. Check out the APIs in the following link.

.appendTo() API

.each API

Is this what you're after?

$('.container > img').each(function () {
    $(this).parent().append(this);
});

It simply takes the <img> within every container and moves as the first child of the container.

You can use .prepend() instead of append Append insert at the end off a the parent. But prepend insert at the begin from the parent. so then like:

$('.container > p').each(function () {
    $(parent).prepend(this);
});

I made a little example and I hope you mean the same thing...

$(document).ready(function() {
    $('.container > img').each(function() {
        $(this).parent().append(this);
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!