Javascript Can';不要改变背景色
我有一个单选按钮,我想改变背景颜色取决于 用户在javascript中选择的颜色,但checkCUST无线电保持不变 透明的。我建议去查看一下网站,以便更好地了解: 我试着自己去发现,但是找不到Javascript Can';不要改变背景色,javascript,html,css,Javascript,Html,Css,我有一个单选按钮,我想改变背景颜色取决于 用户在javascript中选择的颜色,但checkCUST无线电保持不变 透明的。我建议去查看一下网站,以便更好地了解: 我试着自己去发现,但是找不到 <th id="th" class="erase"><input type="radio" id="ERA" name="colo" /><label for="ERA"></label></th> <th id="th" c
<th id="th" class="erase"><input type="radio" id="ERA" name="colo" /><label
for="ERA"></label></th>
<th id="th" class="black"> <input type="radio" id="BLACK" name="colo" /><label for="BLACK"></label></th>
<th id="th" class="purple"><input type="radio" id="PURPLE" name="colo" /><label for="PURPLE"></label></th>
<th id="th" class="blue"><input type="radio" id="BLUE" name="colo" /><label for="BLUE"></label></th>
<th id="th" class="green"><input type="radio" id="GREEN" name="colo" /><label for="GREEN"></label></th>
<th id="th" class="yellow"><input type="radio" id="YELLOW" name="colo" /><label for="YELLOW"></label></th>
<th id="th" class="orange"><input type="radio" id="ORANGE" name="colo" /><label for="ORANGE"></label></th>
<th id="th" class="red"><input type="radio" id="RED" name="colo" /><label for="RED"></label></th>
<th id="th" class="pink"><input type="radio" id="PINK" name="colo" /><label for="PINK"></label></th>
<th id="th" class="brown"><input type="radio" id="BROWN" name="colo" /><label for="BROWN"></label></th>
<th id="th" class="custom"><input type="radio" id="OK" name="colo"/><label for="OK"></label></th>
<th class="rainbow" onclick="hexa()"></th>
</tr>
</table>
<table class="td">
<tr>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
<th onclick="myFunc(this)"></th>
</tr>
</table>
您的问题本来可以用更好的措辞,您的代码需要更干净,但是 对于这个特殊问题,在用户提交自定义十六进制值后,您没有设置元素颜色,您需要将其应用于单选按钮的父元素 移动这条线
checkCUST.style.backgroundColor = colors;
。。。到该函数的末尾,并添加parentNode,如下所示:
function hexa(){
colors = window.prompt("Enter Hex Value");
// Check if colors isn't empty. Even better to confirm if it's actually a valid hex color.
if (colors != null) {
// Apply the new custom hex value to the element
checkCUST.parentNode.style.backgroundColor = colors;
}
}
对您的代码进行一些改进,也许您正在朝着这个方向努力:
- 使用JS生成网格,而不是硬编码所有这些表格单元格
- 对单元格使用JS事件监听器,而不是反复重复“onclick”
- 您的表格网格应该使用tr和td,而不是th。TH用于标题行。你真幸运,这张桌子无论如何都能用。在任何情况下,可能有更好的方法使用div和flexbox渲染网格
- 所有颜色都应保存在一个数组中,并使用该数组生成颜色“工具栏”
th
时,您正试图更改收音机的背景色。这样:checkCUST.parentNode.style.backgroundColor=colors
@pstanton当我点击烤架时,函数被调用,只是我展示的代码中没有完全无关的部分,但是为了读者的需要,也为了你自己的理智,可以继续将代码切碎到一个合理的大小,将你的例子减少到复制你的问题所需的最低限度。谢谢你的建议。顺便说一句,我照你说的做了,但我没有改变任何东西,它仍然是白色的尝试parentNode
function hexa(){
colors = window.prompt("Enter Hex Value");
// Check if colors isn't empty. Even better to confirm if it's actually a valid hex color.
if (colors != null) {
// Apply the new custom hex value to the element
checkCUST.parentNode.style.backgroundColor = colors;
}
}