【发布时间】:2023-03-15 19:08:01
【问题描述】:
我已经编写了这段代码,我想在每次点击输入时获得一个值。前两个输入正常工作,但 3 没有。我想让 3 按照这个原则工作:当我点击它时,会打开一个选择颜色的窗口,当我选择它时,我会在控制台中获得所选颜色的十六进制颜色。请帮我。 编辑:正如您在小提琴中看到的那样。我希望可以选择黑白并使用输入类型颜色选择颜色。我希望输入类型颜色像其他输入项一样获得一个框架。此外,您可以做些什么来确保当您选择一种颜色时,颜色渐变会变为您选择的颜色渐变?
var radio = document.querySelectorAll('input');
for(i=0;i<radio.length;i++){
radio[i].addEventListener('click',function(){
var color = document.querySelector('input:checked').value;
console.log(color);
});
}
input {
position: absolute;
width: 0;
height: 0;
opacity: 0;
}
label {
display: block;
width: 34px;
height: 34px;
border-radius: 50%;
border: 3px solid #fff;
margin: 2px;
box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1);
}
label:nth-child(2) {
background-color: #000;
}
label:nth-child(4) {
background-color: #fff;
}
label:nth-child(6) {
background: conic-gradient(from 90deg,violet,indigo,blue,green,yellow,orange,red,violet);
}
input:checked + label {
box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1),
0 0 0 1px #000;
}
<input type="radio" name="color" id="color1" value="#000000" checked>
<label for="color1"></label>
<input type="radio" name="color" id="color2" value="#fffffff">
<label for="color2"></label>
<input type="color" name="color" id="color3">
<label for="color3"></label>
【问题讨论】:
标签: javascript html input