首页 > Web开发 > 详细

js交换两个div的位置

时间:2015-08-19 19:55:54      阅读:241      评论:0      收藏:0      [点我收藏+]

demo

js代码

function exchange(el1, el2){
        var ep1 = el1.parentNode,
            ep2 = el2.parentNode,
            index1 = Array.prototype.indexOf.call(ep1.children, el1),
            index2 = Array.prototype.indexOf.call(ep2.children, el2);
        ep2.insertBefore(el1,ep2.children[index2]);
        ep1.insertBefore(el2,ep1.children[index1]);
    }

  代码非常简短,开始以为需要判断被交换的element是否是父层最后一个,但是实际看来,如果insertBefore第二个参数是undefined的话,那么是直接appendChild的。

js交换两个div的位置

原文:http://www.cnblogs.com/childsplay/p/4742966.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!