2017-03-26 5 views
0

対応するボタンをクリックするとスワップする必要があるdivがいくつかあります。jqueryを使用してDOM上で視覚的にクリックしてdivをスワップ

などについては
<html> 
    <head> 
     <style> 
      .box { 
       height: 25%; 
       width: 45%; 
       padding: 1%; 
       margin-left: 1%;  
       margin-top: 1%; 
       border: 1px solid black; 
       float: left; 

      } 

     </style> 
     <script src="css/jquery-3.2.0.js"></script> 
    </head> 
    <body> 
     <div class="container"> 
      <div class="box" id="one"> 
       <p>one</p> 
       <button onclick="moveMe_right()">Swap with right!</button> 
       <button onclick="moveMe_down()">Swap with down!</button> 
      </div> 

      <div class="box" id="two"> 
       <p>two</p> 
       <button onclick="moveMe_left()">Swap with left!</button> 
       <button onclick="moveMe_down()">Swap with down!</button> 
      </div> 

      <div class="box" id="three"> 
       <p>three</p> 
       <button onclick="moveMe_right()">Swap with right!</button> 
       <button onclick="moveMe_top()">Swap with top!</button> 

      </div> 

      <div class="box" id="four"> 
       <p>four</p> 
       <button onclick="moveMe_left()">Swap with left!</button> 
       <button onclick="moveMe_down()">Swap with down!</button> 
      </div> 

     </div> 
    </body> 
</html> 

、私はdivの一つに右でスワップをクリックすると、それはそれはに変更する必要があり、視覚だけでなく、DOM内のdiv 1とdivの2入れ替える必要があります - 同様に

<div class="container"> 
       <div class="box" id="two"> 
        <p>two</p> 
        <button onclick="moveMe_right()">Swap with right!</button> 
        <button onclick="moveMe_down()">Swap with down!</button> 
       </div> 

       <div class="box" id="one"> 
        <p>one</p> 
        <button onclick="moveMe_left()">Swap with left!</button> 
        <button onclick="moveMe_down()">Swap with down!</button> 
       </div> 

       <div class="box" id="three"> 
        <p>three</p> 
        <button onclick="moveMe_right()">Swap with right!</button> 
        <button onclick="moveMe_top()">Swap with top!</button> 

       </div> 

       <div class="box" id="four"> 
        <p>four</p> 
        <button onclick="moveMe_left()">Swap with left!</button> 
        <button onclick="moveMe_down()">Swap with down!</button> 
       </div> 

      </div> 

を、左、上、下のdivのいずれかとスワップするために同じことを達成するにはどうすればいいですか?

答えて

1

これを展開して使用することができます

$(toMove1).insertAfter($(toMove1).next().next()); 

しかし、これはdiv 'one'をdiv 'three'の場所に移動するだけです。その後div 'two'はdiv 'のスロットに、' three 'は' two 'に落ちます。

ただし、divが移動されたら、次に何が起こりますか? たとえば、 'Swap with right'をクリックし続けると、下のdivと左にスワップする必要があります。

私は4つのポジションdiv( 'parent'クラス)を追加しました。そのため、DOMで必要なdivを移動し、各エリア内のラベルなどにルールを使用することができます。私は 'topLeft'、 'bottomRight'などを使用してデモンストレーションしましたが、必要に応じてさまざまな位置とインデックスを使用できます。

以下のコードは、イベントハンドラ、ラベルの変更、コードの削減を選択的に更新するためにリファクタリングすることができますが、何が起こっているかを簡単に確認できるように、

<html> 
<head> 
    <style> 
     .box { 
      height: 25%; 
      width: 45%; 
      padding: 1%; 
      margin-left: 1%; 
      margin-top: 1%; 
      border: 1px solid black; 
      float: left; 
     } 
    </style> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
</head> 
<body> 
    <div class="container"> 
     <div id="topLeft" class="parent"> 
      <div class="box" id="one"> 
       <p>one</p> 
       <button class="right">Swap with right!</button> 
       <button class="down">Swap with down!</button> 
      </div> 
     </div> 
     <div id="topRight" class="parent"> 
      <div class="box" id="two"> 
       <p>two</p> 
       <button class="left">Swap with left!</button> 
       <button class="down">Swap with down!</button> 
      </div> 
     </div> 
     <div id="bottomLeft" class="parent"> 
      <div class="box" id="three"> 
       <p>three</p> 
       <button class="right">Swap with right!</button> 
       <button class="top">Swap with top!</button> 
      </div> 
     </div> 
     <div id="bottomRight" class="parent"> 
      <div class="box" id="four"> 
       <p>four</p> 
       <button class="left">Swap with left!</button> 
       <button class="top">Swap with top!</button> 
      </div> 
     </div> 
    </div> 
<script> 
    $(document).ready(function() { 
     // Set the event handlers on load... 
     resetEvents(); 
    }); 

    function UpdateDivs(parent1, parent2, class1, class2) { 
     var parent1Content = $('#' + parent1).children(); 
     var parent2Content = $('#' + parent2).children(); 

     $(parent1Content).find('.' + class1).each(function() { 
      swapButtonClass(this); 
     }); 

     $(parent2Content).find('.' + class2).each(function() { 
      swapButtonClass(this); 
     }); 

     $('#' + parent1).append(parent2Content); 
     $('#' + parent2).append(parent1Content); 

     resetEvents(); 
    } 

    function resetEvents() { 
     // Clear the current handlers - because the buttons will change their class. 
     // The handlers are still attached to the buttons that were seen with that class initially. 
     // This could be done selectively, but for demo purposes, just resets all of them when the DOM is changed. 
     $('.right').unbind('click'); 
     $('.left').unbind('click'); 
     $('.top').unbind('click'); 
     $('.down').unbind('click'); 

     $('.right').click(function() { 
      var parent1 = $(this).parents('.parent').attr('id'); 
      var parent2 = parent1.replace('Left', 'Right'); 

      UpdateDivs(parent1, parent2, 'right', 'left'); 
     }); 

     $('.left').click(function() { 
      var parent1 = $(this).parents('.parent').attr('id'); 
      var parent2 = parent1.replace('Right', 'Left'); 

      UpdateDivs(parent1, parent2, 'left', 'right'); 
     }); 

     $('.down').click(function() { 
      var parent1 = $(this).parents('.parent').attr('id'); 
      var parent2 = parent1.replace('top', 'bottom'); 

      UpdateDivs(parent1, parent2, 'down', 'top'); 
     }); 

     $('.top').click(function() { 
      var parent1 = $(this).parents('.parent').attr('id'); 
      var parent2 = parent1.replace('bottom', 'top'); 

      UpdateDivs(parent1, parent2, 'top', 'down'); 
     }); 

     $('.container').eq(0); 
    } 

    function swapButtonClass(button) { 
     // Swap class and labels when moving the divs around. 
     switch (button.className) { 
      case "right": 
       $(button).removeClass('right').addClass('left').text($(button).text().replace('right', 'left')); 
       break; 
      case "left": 
       $(button).removeClass('left').addClass('right').text($(button).text().replace('left', 'right')); 
       break; 
      case "top": 
       $(button).removeClass('top').addClass('down').text($(button).text().replace('top', 'down')); 
       break; 
      case "down": 
       $(button).removeClass('down').addClass('top').text($(button).text().replace('down', 'top')); 
       break; 
     } 
    } 
</script> 
</body> 
</html> 
+0

それ以上に完璧です!ありがとうたくさん:) – Sunny

+0

"topLeft"、 "topRight"とは何ですか?これらはいくつかのランダムなIDで置き換えることができますか?これらのIDのテキストを置き換えようとしていることを理解しています。しかし、我々はparent-box-1、parent-box-2などのようなランダムなidを持つparent divを無作為に生成できますか? – Sunny

+0

@サニー、うれしかったよ。はい、あなたは正しいです - あなたは、あなたが右または左にいくつかの場所を移動し、同じ方法で上下に移動できるようにしたい命名規則にこれらを変更することができます。 divの数とその行数に応じて、関連するdivの位置を配列/行列などから取得し、そこに名前(またはインデックス)を格納できるように設定することができます。私はちょうどあなたが現在持っているものをカバーするためにこれらの4つ(ハードコード)を使用しました。 20個のdivがある場合は、独自のインデックス/位置決めアルゴリズムに従ってページの読み込みに名前を付けることができます。 –

0

[OK]を私は左の&右のdivsとスワップするためにこれを持っている。 今、上下にこだわっています!

ここに私の更新されたコードがあります。また

<html> 
    <head> 
     <style> 
      .box { 
       height: 25%; 
       width: 45%; 
       padding: 1%; 
       margin-left: 1%;  
       margin-top: 1%; 
       border: 1px solid black; 
       float: left; 

      } 

     </style> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    </head> 
    <body> 
     <div class="container"> 
      <div class="box" id="one"> 
       <p>one</p> 
       <button class="right">Swap with right!</button> 
       <button class="down">Swap with down!</button> 
      </div> 

      <div class="box" id="two"> 
       <p>two</p> 
       <button class="left">Swap with left!</button> 
       <button class="down">Swap with down!</button> 
      </div> 

      <div class="box" id="three"> 
       <p>three</p> 
       <button class="right">Swap with right!</button> 
       <button class="top">Swap with top!</button> 

      </div> 

      <div class="box" id="four"> 
       <p>four</p> 
       <button class="left">Swap with left!</button> 
       <button class="down">Swap with down!</button> 
      </div> 
     </div> 
     <script> 
      $(document).ready(function() { 
       $('.right').click(function(){ 
        //alert('ok'); 
        var toMove1 = $(this).parents('.box'); 
        //toMove2 = toMove1.next(); 

        $(toMove1).insertAfter($(toMove1).next()); 
       }); 

       $('.left').click(function(){ 
        //alert('ok'); 
        var toMove1 = $(this).parents('.box'); 
        //toMove2 = toMove1.prev(); 

        $(toMove1).insertBefore($(toMove1).prev()); 
       }); 

       $('.container').eq(0); 
       /* 
       $('.down').click(function(){ 
        //alert('ok'); 
        toMove1 = $(this).parents('.box'); 
        //toMove2 = toMove1.prev(); 

        var toMove2 = $(toMove1).insertAfter($(toMove1).next()); 
        $(toMove2).insertAfter($(toMove2).next()); 
       }); 

       $('.top').click(function(){ 
        //alert('ok'); 
        toMove1 = $(this).parents('.box'); 
        //toMove2 = toMove1.prev(); 

        $(toMove1).insertAfter($(toMove1).prev()); 
       }); 



       $(".box").first().css({"background-color":"yellow"}); 

       $('.box:first-child').eq(3).removeClass('.left'); 
       $('.box:first-child').eq(3).addClass('.right'); 
       */ 
      }); 
     </script> 
    </body> 
</html> 

、私はそれはと似て右の要素だと場合div要素が交換されたときに右の変更で、ボタンスワップは、各ボタンに応じて、左に変更を交換することを保証しますか?基本的には、のinsertBefore /後ないスワップ divをしますが、それらを一緒に移動を使用して、これは1と交換する左上のdivを取得するには、あなたの現在の方法

$(toMove1).insertAfter($(toMove1).next()); 

を使用することによって証明することができる

+0

最後にあなたのコメントを気付き、divを移動するときのトラッキングを含めるように答えを更新しました。希望が役立ちます。 –

1

あなたはこの問題に対して複数の質問をしているようです。
私は以前のquestion hereに答えました。スワップについては

jsfiddle here


コード

function resetButtons() { 
    // enable and show all buttons 
    $("button").prop("disabled", false).show(); 

    // First box (top left), disable and hide top and left 
    var firstBox = $(".box").eq(0); 
    firstBox.find(".top").prop("disabled", true).hide(); 
    firstBox.find(".left").prop("disabled", true).hide(); 

    // Second box (top right), disable and hide top and right 
    var secondBox = $(".box").eq(1); 
    secondBox.find(".top").prop("disabled", true).hide(); 
    secondBox.find(".right").prop("disabled", true).hide(); 

    // Third box (bottom left), disable and hide down and left 
    var thirdBox = $(".box").eq(2); 
    thirdBox.find(".down").prop("disabled", true).hide(); 
    thirdBox.find(".left").prop("disabled", true).hide(); 

    // Fourth box (bottom right), disable and hide down and right 
    var fourthBox = $(".box").eq(3); 
    fourthBox.find(".down").prop("disabled", true).hide(); 
    fourthBox.find(".right").prop("disabled", true).hide(); 
} 

、我々はボックスの配列インデックスで再生すると、各ボックスのHTMLコンテンツを交換します。

function swapContent(divA, divB) { 
    var tempDiv = divA.html(); 
    divA.html(divB.html()); 
    divB.html(tempDiv); 
} 

例えば、右ボタン現在ボックスと(右側に)横に1を交換します。

$(".container").on("click", ".right", function(e) { 
    var currentBox = $(this).parents('.box'); 
    var currentIndex = $(".box").index(currentBox); 
    console.log(currentIndex, "right", currentBox); 

    swapContent(
    currentBox, 
    $(".box").eq(currentIndex+1) 
); 

    resetButtons(); 
}); 
関連する問題