jQuery move node out of its parent

后端 未结 3 1064
面向向阳花
面向向阳花 2020-12-06 19:30

I have this code:

  • SOME TEXT
相关标签:
3条回答
  • 2020-12-06 19:50

    Try this:

    $('.list > li > a > img').each(function() {
        $(this).insertBefore($(this).parent());
    })
    

    Demo at http://jsfiddle.net/alnitak/3nEVz/

    EDIT I came up with a cleaner version:

    $('.list > li > a > img').each(function() {
        $(this).parent().before(this);
    })
    
    0 讨论(0)
  • 2020-12-06 19:59

    Try this:

    $(function() {
        $('ul.list > li > a > img').each(function() {
          $(this).closest('li').prepend(this);
        });
    });
    

    See it in action here.

    0 讨论(0)
  • 2020-12-06 20:04
    $('ul.list img').each(function(_, elem) {
        var $elem = $(elem);
        $elem.prependTo( $elem.closest('li') );
    });
    

    demo: http://jsfiddle.net/hPbZD/1/

    0 讨论(0)
提交回复
热议问题