|
@@ -0,0 +1,55 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <div class="container">
|
|
|
+ <div class="list-item">
|
|
|
+ <span>div1</span>
|
|
|
+ <button class="up-btn">向上按钮</button>
|
|
|
+ <button class="down-btn">向下按钮</button>
|
|
|
+ </div>
|
|
|
+ <div class="list-item">
|
|
|
+ <span>div2</span>
|
|
|
+ <button class="up-btn">向上按钮</button>
|
|
|
+ <button class="down-btn">向下按钮</button>
|
|
|
+ </div>
|
|
|
+ <div class="list-item">
|
|
|
+ <span>div3</span>
|
|
|
+ <button class="up-btn">向上按钮</button>
|
|
|
+ <button class="down-btn">向下按钮</button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <script>
|
|
|
+ var upBtn = document.getElementsByClassName("up-btn");
|
|
|
+ var downBtn = document.getElementsByClassName("down-btn");
|
|
|
+
|
|
|
+ var oContainer = document.getElementsByClassName("container")[0];
|
|
|
+ for(var i=0;i<upBtn.length;i++){
|
|
|
+ downBtn[i].onclick = function(){
|
|
|
+ var oParent = this.parentElement;
|
|
|
+ var nextParent = oParent.nextElementSibling;
|
|
|
+ // console.log(nextParent);
|
|
|
+ if(nextParent){
|
|
|
+ oContainer.insertBefore(nextParent,oParent)
|
|
|
+ }else{
|
|
|
+ console.log("到底了")
|
|
|
+ }
|
|
|
+ // js中除了 undefind null ""(空字符) 0 false 其他全为真;
|
|
|
+ }
|
|
|
+ upBtn[i].onclick = function(){
|
|
|
+ var oParent = this.parentElement;
|
|
|
+ var preParent = oParent.previousElementSibling;
|
|
|
+ if(preParent){
|
|
|
+ oContainer.insertBefore(oParent,preParent);
|
|
|
+ }else{
|
|
|
+ console.log("到顶了")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|