JavaScript moving element in the DOM

后端 未结 7 1688
囚心锁ツ
囚心锁ツ 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:40

    Trivial with jQuery

    $('#div1').insertAfter('#div3');
    $('#div3').insertBefore('#div2');
    

    If you want to do it repeatedly, you'll need to use different selectors since the divs will retain their ids as they are moved around.

    $(function() {
        setInterval( function() {
            $('div:first').insertAfter($('div').eq(2));
            $('div').eq(1).insertBefore('div:first');
        }, 3000 );
    });
    

提交回复
热议问题