<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<style>
div{
width: 100px;
height: 100px;
margin: 10px;
}
#box1{
background: green;
}
#box2{
background: burlywood;
}
</style>
<div>
<div id="box1">
1
</div>
<div id="box2">
2
</div>
</div>
</body>
<script>
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]);
}
exchange(document.getElementById("box1"),document.getElementById("box2"))
</script>
</html>
07-02
289