我想通過jQuery更改div的CSS。以下是jQuery代碼。從jQuery更改CSS
$(".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代碼,請告訴我,我很樂意提供。如果可以的話請幫忙。謝謝。
Pleaseee help .......
在第二個CSS調用''background-color「。」red「中有一個點,你的if語句應該用比較運算符'=='測試,而不是賦值運算符'=' – Stphane 2013-02-25 16:05:07
你有沒有檢查了Firebug中的錯誤控制檯? – HerrSerker 2013-02-25 16:07:33
民意調查真的有一個正確的答案? :) – Andbdrew 2013-02-25 16:10:43