这似乎是一个简单的问题,但没有任何解决办法。我正在尝试使用javascript / jQuery动态更改某些文本的背景颜色(从白色或粉红色变为绿色),但由于某种原因,它无法正常工作。文本使用称为“.novice”的CSS类设置样式。
这是CSS。这很简单。我还尝试了完全删除背景色,因此它尚未设置背景色。
<style type="text/css">
.novice {
background-color: pink;
}
</style>
这是一个数组,其中包含我使用循环写出的项目。第一项为“新手”类
var achievements = ['<span class="novice">novice - 10 or more guesses </span>', ...]
下面是一个if语句,如果为true,则应使“.novice”类具有“background-color:green”属性,并用绿色突出显示“novice-10个或更多的猜测”。我很肯定我已经正确设置了变量timeguessed并且拼写正确。但是,当猜测的时间大于10时,“新手...”仍不会以绿色突出显示。
if (timesguessed > 10) {
$('.novice').css('background-color', 'green');
}
我可以在上方输入此内容吗?
我也尝试替换$('。novice')。css('background-color','green');与
$('。novice')。background-color(green); ,尽管那可能是错误的。
即使我用所谓的新修改的“新手”类打印出另一行,该文本仍不会以绿色突出显示。
document.write('<span class="novice">novice - 10 or more guesses </span>');
我知道原始CSS .novice类正在工作,因为无论时间猜测是大于还是小于10,文本都会以粉红色突出显示。
我不确定Javascript是否没有修改CSS类,或者是什么。也许它只是其他方面的事情压倒了它?
谢谢您的帮助。是的,我是javascript / jQuery的初学者。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.novice {
}
</style>
<script type="text/javascript" src="../MM_JAVASCRIPT2E/MM_JAVASCRIPT2E/_js/jquery-1.6.3.min.js"></script>
<title>Number Guessing Game</title>
</head>
<body>
<h1 style="text-align:center;"> Number Game </br> Try to guess my number that is between 0-1000 </h1>
<script>
// BEGIN LONG BLOCK OF CODE WITH GUESSING GAME MECHANISMS. You can ignore this part I think, this part works.
var realnumber = prompt('Player 1, please enter a number for Player 2 to guess then hand it off to Player 2.', '');
while (isNaN(realnumber)) {
realnumber = prompt('Player 1, please enter a NUMBER, dimwit, for Player 2 to guess.', '');}
var timesguessed=0;
while (numbertoguess != 0) {
var numbertoguess = prompt("Player 2, guess a number", "");
while (isNaN(numbertoguess)) {
numbertoguess = prompt('Please, type in a number');} // why don't I need an "else" here?
numbertoguess = Math.abs(numbertoguess - realnumber);
if ( numbertoguess >= 50 ) {
alert("Tundra cold");
timesguessed++;
}
else if ( 30 <= numbertoguess && numbertoguess < 50) {
alert("cold");
timesguessed++;
}
else if ( 20 <= numbertoguess && numbertoguess < 30 ) {
alert("warm");
timesguessed++;
}
else if ( 10 <= numbertoguess && numbertoguess< 20 ) {
alert("hot");
timesguessed++;
}
else if ( 5 <= numbertoguess && numbertoguess < 10 ) {
alert("Steaming hot!");
timesguessed++;
}
else if ( 3 <= numbertoguess && numbertoguess < 5 ) {
alert("SCALDING HOT!");
timesguessed++;
}
else if ( 1 < numbertoguess && numbertoguess < 3 ) {
alert("FIRE!");
timesguessed++;
}
else if ( numbertoguess == 1 ) {
alert("Face Melting!");
timesguessed++;
} else if ( numbertoguess == 0 ) {
alert("BINGO!");
timesguessed++;
}
}
document.write('</br></br></br></br><h2 style="text-align:center; font-size: 18px;"> The number was ' + realnumber + '.');
if (timesguessed == 1) {
document.write('</span><h2 style="text-align:center;">It took you ' + timesguessed + ' guess.</h2>');
} else {
document.write('<h2 style="text-align:center;">It took you ' + timesguessed + ' guesses.</h2>');
}
// END LONG BLOCK OF CODE WITH GUESSING GAME MECHANISMS
document.write('</br></br>')
//below is the array written out with a loop
var achievements = ['<span class="novice">novice - 10 or more guesses </span>',bronze - 7-10 guesses', 'silver', 'gold', 'titanium', 'platinum', 'diamond', ]
var counter = 0;
while (counter < achievements.length) {
document.write('<h2 style="text-align:center;">' + achievements[counter] + ' ');
counter++;
}
//below is the "if" function of question
if (timesguessed > 10) {
$('.novice').css('background-color', '#00FF00'); //why does this not work?
}
document.write('<span class="novice">novice - 10 or more guesses </span>'); //why does this not work?
</script>
</body>
</html>
最佳答案
jQuery未正确包含在页面中。检查控制台将告诉您链接是否失败(并且还应说$
未定义)。要使用Google的最新版本,请使用此链接:http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
即更改此:
<script type="text/javascript" src="../MM_JAVASCRIPT2E/MM_JAVASCRIPT2E/_js/jquery-1.6.3.min.js"></script>
对此
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
在 undefined object 上调用方法将导致错误,并且后续代码的执行将失败。