Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:更改假答案单选按钮的背景色_Javascript_Html_Radio Button - Fatal编程技术网

Javascript:更改假答案单选按钮的背景色

Javascript:更改假答案单选按钮的背景色,javascript,html,radio-button,Javascript,Html,Radio Button,函数radioResult(){ var r=document.getElementsByName('0'); 对于(变量i=0;i

函数radioResult(){
var r=document.getElementsByName('0');
对于(变量i=0;i
a
b
c
d

结果
您可以参考

Html

<div><input type='radio' name='0' value='1'>a</div>
<div><input type='radio' name='0' value='2'>b</div>
<div><input type='radio' name='0' value='3'>c</div>
<div><input type='radio' name='0' value='4'>d</div>    
<hr>
<button id="Mcqsbtn" onclick="radioResult()">Result</button> 
a
B
C
D

结果
Javascript

function radioResult(){
    var r = document.getElementsByName('0');
    for(var i = 0; i < r.length; i++){        
        if(r[i].checked){
            rValue = r[i].value;
            if(rValue!='1') {
                r[i].parentNode.style.backgroundColor='blue';
            } else {
                alert('correct');
            }
        }
    } 

}
函数radioResult(){
var r=document.getElementsByName('0');
对于(var i=0;i
请参阅

解释


您必须设置特定单选按钮的背景色。因此,您不应该使用
r.style.backgroundColor='blue'
而是应该使用
r[i].style.backgroundColor='blue'

首先,您要以实际元素为目标,即

 r[i].style.backgroundColor='blue';
这会将属性应用于元素,但单选按钮的设计不具有背景色。我建议您将每个按钮包装在一个带有一些填充的div中,并改为设置div的样式

函数radioResult(){
var r=document.getElementsByName('0');
对于(变量i=0;i
.wrap{
填充物:5px;
}

a
b
c
d


结果
以元素为目标:

 r[i].parentElement.style.backgroundColor='YourColor';

您无法如此轻松地更改radio输入元素的css属性。检查这个:你需要标签,还有一些其他的东西。没问题。很乐意帮忙。