简体   繁体   English

我想访问具有相同名称或 ID 的函数并使用 javascript 更改它们的样式

[英]I Want to access function with same name or id and changer their style with javascript

This are my options for which text i want to change the style这是我要更改样式的文本的选项

<form id="question29">
<input type="radio" onclick="cbg()" name = "q30" value="A"><div name="ques29">(A) wavelength</div>
<br><input type="radio" onclick="cbg()" name = "q30" value="B"><div name="ques29">(B) frequency</div>
<br><input type="radio" onclick="cbg()" name = "q30" value="C"><div name="ques29">(C) intensity</div>
<br><input type="radio" onclick="cbg()" name = "q30" value="D"><div name="ques29">(D) work function</div>
</form>

Now i want to change the color of suppose options (D) work function to green so how can i do that well i have defined a class qa to do this现在我想将假设选项(D)工作功能的颜色更改为绿色,所以我怎么能做得好我已经定义了一个类 qa 来做到这一点

.qa {
color:green;
font-size:200%;
}

and then i have written this code然后我写了这段代码

document.getElementByName("ques29")[1].className = "qa"; 

well i am able to change the color if i assign different names to everyone but unfortunately i need some way to access the options in a format of somename[0] somename[1] somename[2] and somename[3]好吧,如果我为每个人分配不同的名称,我可以更改颜色,但不幸的是,我需要某种方式来访问 somename[0] somename[1] somename[2] 和 somename[3] 格式的选项

any suggestions thankx in advance任何建议提前谢谢

Change getElementByName() to getElementsByName() (with an 's') to solve the issue of the code not working.getElementByName()更改为getElementsByName() (带有“s”)以解决代码不起作用的问题。

In response to your comment, simply removing the div 's and replacing them with labels 's should still behave how you want.为了回应您的评论,只需删除div并用labels替换它们应该仍然表现得如您所愿。

EDIT:编辑:

Your markup should look like this:您的标记应如下所示:

<form id="question29">
    <input type="radio" onclick="cbg()" name = "q30" value="A"><label name="ques29">(A) wavelength</label>
    <br><input type="radio" onclick="cbg()" name = "q30" value="B"><label name="ques29">(B) frequency</label>
    <br><input type="radio" onclick="cbg()" name = "q30" value="C"><label name="ques29">(C) intensity</label>
    <br><input type="radio" onclick="cbg()" name = "q30" value="D"><label name="ques29">(D) work function</label>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM