JavaScript moving element in the DOM

后端 未结 7 1678
囚心锁ツ
囚心锁ツ 2020-11-27 12:25

Let\'s say I have three

elements on a page. How can I swap positions of the first and third
? jQuery is fine.

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 12:59

    Sorry for bumping this thread I stumbled over the "swap DOM-elements" problem and played around a bit

    The result is a jQuery-native "solution" which seems to be really pretty (unfortunately i don't know whats happening at the jQuery internals when doing this)

    The Code:

    $('#element1').insertAfter($('#element2'));
    

    The jQuery documentation says that insertAfter() moves the element and doesn't clone it

提交回复
热议问题