javascript - 显示输入颜色选择器时无法输入输入文本
问题描述
我试图在有人在文本框上键入内容的同时打开选择器。但是当它打开时会发生什么,我无法在文本框中输入。这是正确的方法吗?还是有更好的方法?
function openPicker(e) {
e.nextElementSibling.click();
}
.color-picker-container {
cursor: pointer;
background-color: transparent;
flex-grow: 1;
border-radius: 5px;
max-height: 15px;
padding: 5px;
position: relative;
width: 200px;
display: flex;
align-items: center;
justify-content: center;
}
.color-picker-text-field {
background-color: #fff;
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
color: #4e4e4e;
max-height: 15px;
padding: 4px;
font-size: 12px;
min-width: 0px;
margin: 0px !important;
border: inherit !important;
box-shadow: 0 0 10px #dadada;
width: 50%;
}
.color-picker-field {
margin: 0px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
max-height: 15px;
-webkit-appearance: none;
border: none;
cursor: pointer;
padding: 0px;
width: 50px;
border-left: solid 3px #f0ecf3;
}
<div class="color-picker-container " style="">
<input type="text" class="color-picker-text-field" value="#ffffff" onclick="openPicker(this);">
<input class="color-picker-field primary-color-picker" type="color" value="#ffffff">
</div>
解决方案
input
如果其他一些输入处于该focus
状态,则您无法与 交互。所以删除下面的行,因为您可以输入第一个input
,也可以同时在第二个输入上选择颜色。如果color
输入处于focus
状态(当您单击first
输入时),则您无法输入,因为它正在等待用户输入。
e.nextElementSibling.click();
当上面的代码被执行时,第二个(颜色)输入将处于focus
状态,这将限制另一个输入处于焦点状态,直到用户输入值或输入丢失它的focus
.
状态中只能有一个元素
focus
。:焦点是元素能够接受输入的时间 - 输入框中的光标或已被选项卡指向的链接。
如果要设置color
输入的默认值,请使用value
<div class="color-picker-container " style="">
<input class="color-picker-field primary-color-picker" type="color" value="#19d763">
</div>
推荐阅读
- javascript - “DataTables 警告:无效的 JSON 响应”和“错误:未找到图形容器元素”
- javascript - 查找在数组中出现奇数次的 int
- javascript - Php - 使星号之间的文本变为粗体
- algorithm - 这可以近似吗?
- python-3.x - XPath 获取类中的一组特定元素
- angular - 错误类型错误:无法读取未定义的属性“getMonth”
- jquery - LayerSlider 启动触发器
- jquery - 每个 item.hover() 的不同背景颜色动画
- php - 当我上传文件时,Laravel 没有获取文件
- swift - 如何使用 Swift 实时显示命令的输出?