2017-02-27 9 views
1

現在、マッチ2アプリを作成中です(マッチする画像を見つけてください) 私は1つの部分で問題が発生しています。クリックイベントを無効にすると、特定の四角形をクリックしてリセットするまでクリックできなくなります。私はreplaceWithを試してみましたが、その問題は一度実行すると、attrを使用してもイベントが再度実行されないことです。私は試しましたが、それはちょうどそれを破るようです。クリックイベントを削除する

HTMLスニペット:

<table border="1"> 
     <tr> 
      <td id="" colspan="3">Lifes Remaining:</td> 
      <td id="lives"></td> 
     </tr> 
     <tr id="1to4"> 
      <td class="a1" id="a1">0</td> 
      <td class="a2" id="a2">0</td> 
      <td class="a3" id="a3">0</td> 
      <td class="a4" id="a4">0</td> 
     </tr> 
     <tr> 
      <td colspan="4"><input type="button" id="press" value="Press" /></td> 
     </tr> 
</table> 

コードスニペット:

var set1a = 1, set1b = 1, set2a = 2, set2b = 2; 

var rand1 = 0; 
var rand2 = 0; 

var lives; 
var savedData1 = "", savedData2 = ""; 
var check1 = "", check2 = ""; 
var idCheck1 = "", idCheck2 = ""; 
$(document).ready(function() 
{ 
    $("#lives").text(""); 
    $("#press").click(function() 
    { 
     lives = 3; 
     $("#lives").text(lives); 

     for(var i = 1; i < 5; i++) 
     {$("#a"+i).text("0").css('color', 'black');} 
     i = 0; 
<!-----------------Set Position--------------------------------->    
     do{rand1 = 1 + Math.floor(Math.random() * 4);} 
     while($("#a"+rand1).text() != 0) 
     $("#a"+rand1).text(set1a); 

     do{rand2 = 1 + Math.floor(Math.random() * 4);} 
     while($("#a"+rand2).text() != 0) 
     $("#a"+rand2).text(set1b); 

     do{rand1 = 1 + Math.floor(Math.random() * 4);} 
     while($("#a"+rand1).text() != 0) 
     $("#a"+rand1).text(set2a); 

     do{rand2 = 1 + Math.floor(Math.random() * 4);} 
     while($("#a"+rand2).text() != 0) 
     $("#a"+rand2).text(set2b); 
<!-----------------Set Position--------------------------------->      
    }); 
     $("#a1").click(function() 
     { 
      $(".a1").css('color', 'lightgreen'); 

      if(check1 == "") 
       check1 = $("#a1").text(); 
      else 
       check2 = $("#a1").text(); 

      if(idCheck1 == "") 
       idCheck1 = "#a1"; 
      else 
       idCheck2 = "#a1" 

      if(check1 && check2 != "") 
      { 
       if(check1 != check2) 
       { 
        $(".a1").css('color', 'black'); 
       } 
      } 
     }); 

     $("#a2").click(function() 
     { 
      $(".a1").css('color', 'lightgreen'); 
      check1 = '1'; 
      if(idCheck1 == "") 
      { 
       idCheck1 = "#a1"; 
      } 
      else 
       idCheck2 = "#a1" 

      if(check1 && check2 != "") 
      { 
       $(".a1").css('color', 'black'); 
      } 
     }); 

     $("#a3").click(function() 
     { 

     }); 

     $("#a4").click(function() 
     { 

     });  
    }); 

私は私はある数のクリック機能を無効にすることができます#A1-4クリック機能で使用するものが必要別の番号がタップされるまでタップしてから再び有効にします。

$("#a1").click(function() 
    { 
     if(check1 == "") 
      check1 = $("#a1").text(); 
     else 
      check2 = $("#a1").text(); 

     if(idCheck1 == "") 
      idCheck1 = "#a1"; 
     else 
      idCheck2 = "#a1" 

     *Disable #a1 click function*  
     if(check1 && check2 != "") 
     { 
      if(check1 != check2) 
      { 
       $(".a1").css('color', 'black'); 
       *Enable #a1 click function* 
      } 
     } 
    }); 

すべてのヘルプなどの

何かがappricated、あなたが任意の明確化が必要な場合、私はクリックイベントリスナーを削除します

マイク

+0

あなたはアンバインドしようとしたバインド http://api.jquery.com/unbind/ http://api.jquery.com/bind/ています –

答えて

0

$('#a1').unbind('click')をお知らせすることでしょう。

http://api.jquery.com/unbind/

http://api.jquery.com/off/

https://stackoverflow.com/a/210345/5758328

+0

バインドが解除されました。私はそれを試したと確信していましたが。もう一度それをバインドする方法?私はあなたが私をリンクしたスタックオーバーフローの答えで、それはちょうどまっすぐなコード – Mike

+0

.bind()または.click againまたは.onにリンクされたSO答えに見るより多くの機能です。 – Mawcel

関連する問題