[英]radio button change color
I am new at javascript,and i was trying and trying with this but something is wrong,i wanna make the checked radio button red, black, green and blue but I don't know how to use JavaScript我是 javascript 的新手,我一直在尝试,但出了点问题,我想让选中的单选按钮变成红色、黑色、绿色和蓝色,但我不知道如何使用 JavaScript
<table> <B id="grandezza1">Colore Testo:</B> </table> <p style="color:blue" id="colore"> <br> Nero   <INPUT type="radio" name="colore" value="Nero" onclick="cambiaColore()" checked> <BR> Rosso <INPUT type="radio" name="colore" value="Rosso" onclick="cambiaColore()"> <BR> Verde <INPUT type="radio" name="colore" value="Verde" onclick="cambiaColore()"> <BR> Blue   <INPUT type="radio" name="colore" value="Blue" onclick="cambiaColore()"> <BR> </P>
function cambiaColore(){
document.getElementById("area").style.color = $('input[name="radioC"]:checked').val();
}
You cannot change the colour of a radio button.您不能更改单选按钮的颜色。 You can change the colour of the label
您可以更改 label 的颜色
In your code, you are mixing DOM (document....) with jQuery ($(...)) that is not a great idea.在您的代码中,您将 DOM (document....) 与 jQuery ($(...)) 混合,这不是一个好主意。 You were also missing the jQuery library.
您还缺少 jQuery 库。
Here I delegate the click from the container在这里,我委托来自容器的点击
I also fixed your invalid HTML.我还修复了您无效的 HTML。
const cambiaColore = () => { document.querySelectorAll("#colore input[type=radio]").forEach(rad => rad.closest("label").classList.toggle(rad.value, rad.checked)) } document.getElementById("colore").addEventListener("click", cambiaColore); cambiaColore(); // initialise
.Nero { background-color: black; color: white }.Rosso { background-color: red; color: yellow }.Verde { background-color: green; color: yellow }.Blue { background-color: blue; color: yellow }
<b id="grandezza1">Colore Testo:</b> <p style="color:blue" id="colore"> <label>Nero <input type="radio" name="colore" value="Nero" checked /></label><br/> <label>Rosso <input type="radio" name="colore" value="Rosso" /></label><br/> <label>Verde <input type="radio" name="colore" value="Verde" /></label><br/> <label>Blue <input type="radio" name="colore" value="Blue" /></label><br/> </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.