這是JavaScript和HTML和CSS我很新的JavaScript問題是如果語句總是顯示錯誤,即使它不應該如果語句總是假的即使我點擊贏變量的顏色值,數組顏色的第三個指標值的顯示錯誤如果語句總是假的,即使條件在Java腳本中爲真
var colors = [
\t "rgb(255,0,0)",
\t "rgb(255,255,0)",
\t "rgb(0,255,0)",
\t "rgb(0,255,255)",
\t "rgb(0,0,255)",
\t "rgb(255,0,255)"
\t ]
var win =colors[3];
var winclrdisplay=document.querySelector(".winclr");
var square= document.querySelectorAll(".square");
\t winclrdisplay.textContent=" "+ win;
\t
\t for (var i = 0 ;i < square.length ; i++){
\t \t //colors
\t \t square[i].style.background =colors[i];
\t \t
\t \t // click listener
\t \t square[i].addEventListener("click", function(){
\t \t var click = this.style.background;
\t \t if (click == win){
\t \t \t alert("correct");
\t \t }else{
\t \t \t alert("wrong");
\t \t }
\t \t }
\t \t);
\t }
body {
\t background-color: #232323;
}
.square{
\t width: 30%;
\t background: purple;
\t padding-bottom: 30%;
\t float: left;
\t margin: 1.66%;
}
#container {
\t max-width: 600px;
\t margin: 0 auto;
}
h1 {
\t color:white;
}
<html>
<head>
\t <title>color guessing game</title>
\t <link rel="stylesheet" type="text/css" href="colorapp.css">
</head>
<body>
\t <h1>The Great <span class="winclr"></span> colour Game</h1>
\t
<div id="container">
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
<div class="square"></div>
</div>
<script type="text/javascript" src="colorapp.js"></script>
</body>
</html>
你想要做一個'的console.log(點擊)',看看你是什麼樣的價值_actually_得到。 – CBroe
可能的重複[如何比較jQuery/JavaScript中的兩個顏色值?](http://stackoverflow.com/questions/2377696/how-can-i-compare-two-color-values-in-jquery-javascript ) – CBroe
考慮用十六進制代碼編寫顏色,可以將其作爲字符串進行比較 –