2017-03-17 24 views
-2

我正在創建一個tic tac toe勝利條件,所以即時檢查前三個單元格是否相等,以及它們是否不是空的。但即使當我滿足條件時,第二個「if」中的代碼沒有被執行。我的代碼有問題嗎? (r1c1表示第一行第一列)等等。爲什麼不執行兩個if語句中的代碼,即使它們都是真的?

if ($('#r1c1').html() !== "") { 
 
    if (($('#r1c1').html() == $('#r1c2').html()) && ($('#r1c1').html() == $('#r1c3').html())) { 
 
    $("#table").css("display", "none"); 
 
    $("#victory").css("display", "You Won"); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="victory" style="display:none"> 
 
    Congratulations, You Won! 
 
</div> 
 
<table style="display:none" id="table"> 
 
    <tr> 
 
    <td id="r1c1" style="text-overflow"></td> 
 
    <td id="r1c2"></td> 
 
    <td id="r1c3"></td> 
 
    </tr> 
 
    <tr> 
 
    <td id="r2c1"></td> 
 
    <td id="r2c2"></td> 
 
    <td id="r2c3"></td> 
 
    </tr> 
 
    <tr> 
 
    <td id="r3c1"></td> 
 
    <td id="r3c2"></td> 
 
    <td id="r3c3"></td> 
 
    </tr> 
 
</table>

+0

是什麼r1ce? p標籤還是什麼? – Ash

+1

這似乎並不是兩個都是真的。我建議你可能需要修剪()返回的HTML值。另請注意,'You Won'不是CSS'display'屬性的有效設置 –

+1

使用內置於瀏覽器中的功能齊全的調試器逐步執行該代碼語句,檢查您從每次調用中獲得的內容等。清晰地如果它們是相同的,那麼'=='會起作用。 –

回答

0

你的發言很好,我測試了它:https://jsfiddle.net/nyxeen/hqyr7dbv/14/ 什麼行不通是$( 「#勝利」),CSS( 「顯示」, 「你贏了」);應該是$(「#victory」)。css(「display」,「block」);或$(「#victory」)。show();

var isx = 'x'; 
 

 

 

 
\t $("#table td").click(function(){ 
 
    \t $(this).html(isx); 
 
    if(isx=="x")isx="o"; 
 
    else isx="x"; 
 
    if ($('#r1c1').html() !== "") { 
 
    \t \t if (($('#r1c1').html() == $('#r1c2').html()) && ($('#r1c1').html() == $('#r1c3').html())) { 
 
     $("#table").hide(); 
 
     $("#victory").show(); 
 
     } 
 
    } 
 
    });
#table tr td{width:40px;height:40px;border: 1px solid black;background:white;cursor:pointer;};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="victory" style="display:none"> 
 
    Congratulations, You Won! 
 
</div> 
 
<table id="table"> 
 
    <tr> 
 
    <td id="r1c1" style="text-overflow"></td> 
 
    <td id="r1c2"></td> 
 
    <td id="r1c3"></td> 
 
    </tr> 
 
    <tr> 
 
    <td id="r2c1"></td> 
 
    <td id="r2c2"></td> 
 
    <td id="r2c3"></td> 
 
    </tr> 
 
    <tr> 
 
    <td id="r3c1"></td> 
 
    <td id="r3c2"></td> 
 
    <td id="r3c3"></td> 
 
    </tr> 
 
</table>

+0

而只是爲了完成:https://jsfiddle.net/nyxeen/hqyr7dbv/21/ – Nyxeen

+0

非常感謝。這一次我真的很難過。再次感謝人。乾杯 – Dav

相關問題