jQuery: appendTo parent

前端 未结 4 2342
灰色年华
灰色年华 2021-02-19 02:59

I can\'t seem to get the appendTo to work. What do I do wrong?

$(\'div:nth-child(2n) img\').appendTo(parent);

Current markup:

&         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 03:22

    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

提交回复
热议问题