나는 여기에서 작업하고있는 게임을 가지고 있는데, 이는 td
을 변수에 의존하는 색상으로 변경합니다. 이 변수를 무작위로 'lime'또는 'red'두 문자열 사이에서 선택하려면이 부분을 사용하십시오. 문제는 CSS를 td
에 적용하는 것으로, 내가 할 수있는 한 올바르게 수행하고 있지만 작동하지 않는 것 같습니다. JQuery를 사용하여 색상 변경
이 잘못되는 두 가지
$().ready(function(){
//Functions
$('td').click(function(){
if($(this).hasClass('block')) return;
$(this).addClass(color);
$(this).addClass('block');
$(this).css('background-color:'+color);
tilesLeft--;
if(color=='lime'){color='red';}else{color='lime';}
});
//Variables
var color;
var tilesLeft = 9;
//Setup
if(Math.round(Math.random())==0){color='lime';}else{color='red';}
});
//Intervals
setInterval(function(){
$('header').css('color:'+color);
},1);
html, body {
background-color:black;
color:white;
text-align:center;
height: 100%;
}
td {
border: 1px solid white;
margin:1px;
width:30%;height:30%;
}
#board {height:500px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>Tic Tac Toe</header>
<table style='width:100%;height:95%;'>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>
- 가
$(this).css('background-color:'+color);
는 $(this).css('color:'+color);
텍스트 색상 을 변경하지 않는 배경 색상을 변경하지 않습니다
도움을 주셔서 감사합니다!
는 콜론을 제거 쉼표 (,)로 플러스를 교체합니다. –