注意:这只是我必须编写的代码的一部分。共有8个问题,所有问题都具有相同的结构,只是带有适当的“文字”
我的问题是,我有一个HTML测验,如果我单击下面的按钮,我需要正确/错误的答案以相应地更改颜色。我是JS的新手,很抱歉,如果以前已经回答过这个问题。我花了数小时试图找到解决办法,并问了我的一位同学。我觉得我想念的东西可能对其他人来说很明显。
/* What I've already tried:
x Instead of using "" - used those ''
x Tried using the div ID
x I added "var" infront of the document.getElement(...) and removed it again
x Tried document.getElement(..).style.Texttransform.color=".." */
/*The HTML Part*/
<div id= "question7">
<form>
<p> Question 7? </p>
<p><input type = "radio" id = "wrong" name= "question7a" value = "Yes">
Yes a b c <br></p>
<p><input type = "radio" id = "wrong" name= "question7b" value = "No"
>No, a b c <br></p>
</form>
</div>
/* The Button */
<div id= "send" >
<form>
<input id="change_button" type="button" value="Change"
onClick="change();"/>
</form>
</div>
/*The Script Part*/
<script>
function change() {
document.getElementbyID("wrong").style.color ="red";
document.getElementbyID("right").style.color ="green";
}
</script>
最佳答案
<div id= "question7">
<form>
<p> Question 7? </p>
<p id = "wrong" ><input type = "radio" name= "question7a" value = "Yes">
Yes a b c </input></p>
<p id = "right" ><input type = "radio" name= "question7b" value = "No"
>No, a b c </input></p>
</form>
</div>
<div id= "send" >
<form>
<input id="change_button" type="button" value="Change"
onClick="change()"/>
</form>
</div>
确切的语法是
document.getElementById
(注意ById
),然后将id="right"
和id ="wrong"
放在<p>
中,这样就完成了。check this code