Switch positions of 2 divs with jQuery

后端 未结 6 1618
遥遥无期
遥遥无期 2020-12-02 22:45

I\'m wondering if it\'s posible to switch positions of two divs with jQuery.

I have two div like this

STUFF ONE
6条回答
  •  有刺的猬
    2020-12-02 23:26

    Here's an example:

    http://jsfiddle.net/52xQP/1/

    First you want to clone the elements. Then, check a condition if div2 is empty. Then, do the swap:

    div1 = $('#div1');
    div2 = $('#div2');
    
    tdiv1 = div1.clone();
    tdiv2 = div2.clone();
    
    if(!div2.is(':empty')){
        div1.replaceWith(tdiv2);
        div2.replaceWith(tdiv1);
    
        tdiv1.addClass("replaced");
    }
    

提交回复
热议问题