【发布时间】:2013-02-25 16:03:12
【问题描述】:
我正在尝试通过 jQuery 更改 div 的 CSS。下面是jQuery代码。
$(".endPollingButton").click(function () {
var endPollingCode = $(this).attr("value");
var correctAnswer = parseInt($(this).attr("id"));
for(LCV=1;LCV<=5;LCV++) {
if(LCV == correctAnswer) {
$("#"+endPollingCode).children(".Bars_"+LCV).css("background-color","green");
alert("check");
} else {
$("#"+endPollingCode).children(".Bars_"+LCV).css("background-color","red");
alert("check");
}
}
});
这里是html代码:
for($LCV = 1; $LCV <= $rowCount; $LCV++) {
echo "<div class='graphBlock' id='".$questionCode[$LCV]."' value=''>";
echo "<div id='questionCodeBar'>Quiz Code: ".$questionCode[$LCV]."</div>";
echo "<div id='questionTitleBar'>".$questionName[$LCV]."</div>";
echo "<span class='barsInfo' id='barsInfo1".$LCV."'></span><div class='bars_1' id='bars1".$LCV."' style='width:;'></div>";
echo "<span class='barsInfo' id='barsInfo2".$LCV."'></span><div class='bars_2' id='bars2".$LCV."' style='width:;'></div>";
echo "<span class='barsInfo' id='barsInfo3".$LCV."'></span><div class='bars_3' id='bars3".$LCV."' style='width:;'></div>";
echo "<span class='barsInfo' id='barsInfo4".$LCV."'></span><div class='bars_4' id='bars4".$LCV."' style='width:;'></div>";
echo "<span class='barsInfo' id='barsInfo5".$LCV."'></span><div class='bars_5' id='bars5".$LCV."' style='width:;'></div>";
echo "<button class='endPollingButton' id='".$correctAnswer[$LCV]."' value='".$questionCode[$LCV]."' >End Polling</button>";
echo "</div>";
}
上面的代码不起作用。我不确定是否有任何语法错误。两个检查警报都有效。如果您需要更多代码,例如链接到的 HTML,请告诉我,我很乐意提供。如果可以的话请帮忙。谢谢。
请帮忙.......
【问题讨论】:
-
第二个 css 调用中有一个点
"background-color"."red"并且您的 if 语句应该使用比较运算符==进行测试,而不是赋值运算符= -
你检查过 Firebug 中的错误控制台吗?
-
民意调查真的有正确答案吗? :)
-
@f00bar 我已经更改了这些错误,但即使这样,div 的背景颜色也没有改变。顺便说一句,感谢您的帮助!
-
@Guido 什么是 Firebug?谢谢!
标签: jquery html css colors children