[英]How do i change the color of the text in a radio input field
Ok so I am making a test that has a list of questions that contains a list of answers. 好的,所以我正在做一个测试,该测试的问题列表包含答案。 Im trying to make the test change the color of the users wrong answers to highlight red and have the correct answers highlighted green.
我试图使测试改变用户的颜色,错误答案以红色突出显示,而正确答案以绿色突出显示。 I understand how to change the background color of elements.
我了解如何更改元素的背景颜色。 However i do not understand how to change the background color of the inner html of a input element of the radio type.
但是我不明白如何更改单选类型的输入元素的内部html的背景颜色。 When i run this code i think its trying to change the actual radio input.
当我运行这段代码时,我认为它试图更改实际的无线电输入。 Any help is appreciated.
任何帮助表示赞赏。
A sample for HTML test: HTML测试示例:
<ol>
<li ><p id = "question 1">What are the three main areas of the Standard User Interface?</p>
<ul type="none">
<li><input type="radio" name="q1" value="0" /> Header, Banner, Frame, Application Window</li>
<li><input type="radio" name="q1" value="0" /> Content Frame, Homepage, Form </li>
<li><input type="radio" name="q1" value="1" /> Application Navigator, Banner Frame, Content Frame </li>
<li><input type="radio" name="q1" value="0" /> Larry, Moe, and Curly</li>
</ul>
</li>
<li><p id = "question 2">In the User interface, what is the gray toolbar called which allows you to add bookmarks?</p>
<ul type="none">
<li><input type="radio" name="q2" value="0" /> Gauge</li>
<li><input type="radio" name="q2" value="1" /> Edge</li>
<li><input type="radio" name="q2" value="0" /> Remedy</li>
<li><input type="radio" name="q2" value="0" /> Banner</li>
</ul>
</li>
<li><p id = "question 3">What can be captured in an update set?</p>
<ul type="none">
<li><input type="radio" name="q3" value="0" /> Modified CI Rules</li>
<li><input type="radio" name="q3" value="1" /> Business Rules</li>
<li><input type="radio" name="q3" value="0" /> Scheduled Jobs</li>
<li><input type="radio" name="q3" value="0" /> None of these</li>
</ul>
</li>
</ol>
<button id = "finish" onchange = "hide" onclick="finishTest()"> Submit Test </button> <button id = "retry" onclick="reloadPage()"> Try Again?</button>
My javascript Code: 我的JavaScript代码:
function finishTest(){
//There are actually 37 questions on the test so far only included 3
var score = 0;
var totalQuestions = 37;
for(var questionNum = 1; questionNum<=totalQuestions; questionNum++) {
var radios = document.getElementsByName('q'+questionNum);
var uQuestion = document.getElementById("question "+questionNum).innerHTML;
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked && radios[i].value=="1"){
score++;
alert(radios.innerHTML);
radios.innerHTML.style.backgroundColor = "lawngreen";
}else if (radios[i].checked && radios[i].value=="0"){
alert(radios.innerHTML);
radios.innerHTML.style.backgroundColor = "red";
}
}
}
score = parseFloat(score*100/totalQuestions).toFixed(1);
alert("You scored "+score+"%");
document.getElementById('finish').style.visibility='hidden';
}
Just make two css classes "correct" and "wrong". 只需使两个CSS类“正确”和“错误”即可。 Then assign these classes to the corresponding
li
elements. 然后将这些类分配给相应的
li
元素。
li.correct{
color:green;
}
li.wrong{
color:red;
}
The best approach is to use two different classes correct
and wrong
and assign them to answers. 最好的方法是使用
correct
和wrong
两个不同的类,并将它们分配给答案。
Define the style of two CSS classes correct
and wrong
: 定义两个CSS类的样式
correct
与wrong
:
.correct { background-color : green; } .wrong { background-color : red; }
Then in your JavaScript, assign the corresponding classes for each answer (the corresponding <li>
tag) : 然后在您的JavaScript中,为每个答案分配相应的类(相应的
<li>
标记):
yourAnswer.className="correct"; or yourAnswer.className="wrong";
Where yourAnswer
is the <li>
of each selected answer. 其中
yourAnswer
是每个选定答案的<li>
。
Note: As suggested by Chris Baker it would be better to put the input
button and its corresponding text
in a label
together, it's more ergonomic as you can see in the first radio of the DEMO. 注意:如Chris Baker所建议的那样,最好将
input
按钮及其对应的text
放到label
,这更符合人体工程学,就像在DEMO的第一个收音机中看到的那样。
<li>
<label for="q1a">
<input id="q1a" type="radio" name="q1" value="0" /> Answer with a label
</label>
</li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.