[英]Using JQuery To Change Color
I have a game I have been working on here, that will change a td
you click on to a color dependent on a variable. 我在这里一直在开发一款游戏,它将您单击的
td
更改为取决于变量的颜色。 I want this variable to be randomly chosen between two strings, either 'lime' or 'red' I have that part down. 我希望在两个字符串(“石灰”或“红色”)之间随机选择此变量,我将该部分放下。 The problem is applying the CSS to the
td
's, as far as I can tell, I am doing it correctly, but it doesn't seem to work. 问题是,将CSS应用于
td
,据我所知,我做得正确,但是似乎不起作用。
$().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>
The two things that are wrong with this are: 这有两件事是错误的:
$(this).css('background-color:'+color);
$(this).css('background-color:'+color);
is not changing the background color $(this).css('color:'+color);
$(this).css('color:'+color);
is not changing the text color Thanks for the help! 谢谢您的帮助!
Not quite right: 不太正确:
$(this).css('background-color', color);
The above, perhaps obviously, assumes that the the color
variable is successfully set to a valid CSS color-string. 上面的内容,显然是假定
color
变量已成功设置为有效的CSS颜色字符串。
It seems you were trying to pass a string, perhaps to the style
attribute; 似乎您正在尝试将字符串传递给
style
属性; instead the css()
method offers two approaches, a 'property','value'
: 相反,
css()
方法提供了两种方法,即'property','value'
:
css('background-color', 'red');
Or an object of property-values: 或具有属性值的对象:
css({
'background-color' : 'red'
});
References: 参考文献:
Here you are, change 在这里,改变
$(this).css('background-color:'+color);
to 至
$(this).css({
backgroundColor: color
});
and 和
$('header').css('color:'+color);
to 至
$('header').css({
color: color
});
Put your interval inside the ready state :) 将您的时间间隔置于就绪状态:)
And it will work :) 它将起作用:)
$(document).ready(function() { //Functions $('td').click(function() { if ($(this).hasClass('block')) return; $(this).addClass(color); $(this).addClass('block'); $(this).css({ backgroundColor: 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.