javascript - HTML 测验 - JavaScript 按钮(将结果变为绿色,假红色)
问题描述
注意:这只是我必须编写的代码片段。大约有 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
(notice to ById
) 然后把id="right"
and id ="wrong"
in<p>
这样你就完成了。
检查此代码
推荐阅读
- python - 如何定期自动运行 Jupyter Notebook 单元
- ios - 两个不同登录名的 UIWebView
- php - 按钮仅适用于第一行
- angular - 同时测试 Angular 库,包括库组件和示例应用程序
- apache - 如何在支持 SSL 的 Amazon EC2 单实例上托管多个网站?
- react-native - 在标签离开时对本机标签重置做出反应
- c# - 我已经使用了参数,但我很难用多用户登录
- asp.net-mvc - 如果我的基本控制器具有处理 dbcontext 的覆盖 void Dispose(),我是否需要在子控制器中处理它?
- c# - 如果 admob 广告未加载统一如何加载自定义横幅广告
- android - 我可以使用普通视图作为小部件吗?