gpt4 book ai didi

javascript - 如何仅使用 JavaScript 切换输入的焦点?

转载 作者:行者123 更新时间:2023-12-02 01:39:22 25 4
gpt4 key购买 nike

我一直在尝试制作这个东西,当单击其外部 div 时,将输入集中在其中。我真的希望这不是重复的,因为我已经到处搜索但没有找到答案。我有被屏蔽的风险,所以我希望没有人将此标记为重复。

这是下面的代码,它可以使输入聚焦,但是当它必须取消聚焦时,它似乎不起作用。

window.onload = function() {
var drop = document.getElementsByClassName("datadrop");
var drops = document.getElementsByClassName("datadrop").length;

for (x = 0; x < drops; x++) {
var input = document.getElementById(drop[x].getAttribute("input"));
drop[x].onclick = function(e) {
e.preventDefault();
clicked = 0
if (this == document.activeElement) {
input.blur();
} else {
input.focus();
}
}
}
}
.datadrop {
padding: 7.5px;
width: 85px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}

.datadrop .drop {
width: 150px;
position: fixed;
display: none;
}

.datadrop .item {
padding: 10px;
}

.datadrop .item:hover {
background-color: #F1F3F4;
}

.datadrop .divider {
padding: 5px;
color: grey;
}

.datadrop .divider:hover {
background-color: #ffffff;
}

.datadrop input {
width: 60px;
color: black;
}

.datadrop:hover {
background-color: #F1F3F4;
color: #5F6368;
}

.datadrop .click {
color: #5F6368;
}

.datadrop input {
background-color: transparent;
border: 1px solid transparent;
outline: none;
padding: 5px;
}

.datadrop input:focus {
background-color: white;
border: 1px solid black;
border-radius: 2.5px;
}
<div datadrop="scale" input="scales" class="datadrop">
<input type="text" id="scales" value="100%"> &nbsp;&nbsp;|&nbsp; <i class="fa-solid fa-caret-down click"></i>
<div class="drop" id="scale">
<div class="item">Fit</div>
<div class="item divider">
<hr>
</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />

我希望有人仅使用 JavaScript 就能找到这个问题的答案,并且我希望没有人将此标记为重复,因为我正在努力不重复。

最佳答案

我编辑了你的代码片段。你使用了一个循环,所以我认为你想要有多个输入。顺便说一下,请注意该元素没有“input”属性。我尝试遵循您的代码逻辑,因此我用数据输入属性替换了“input”-wrong- 属性。

我使用两个标志来检查输入是否被选中:“inputIsSelected”和“selectedDrop”

如果 inputIsSelected 并单击相同的父模糊如果 inputnotSelected 且父焦点相同如果父级更改,则将 inputIsSelected 设置为 false

window.onload = function() {
var drop = document.getElementsByClassName("datadrop");
var drops = document.getElementsByClassName("datadrop").length;
let selectedDrop = null // a flag the clicked drop
let inputIsSelected = false;
console.log({drops})
var x;


for (x = 0; x < drops; x++) {
//var input = document.getElementById(drop[x].getAttribute("input"));

let inputId = drop[x].dataset.input; //select inputId using a data-* attribute

let input = document.getElementById(inputId);

drop[x].onclick = function(e) {

e.preventDefault();
clicked = 0

if(this!==selectedDrop){
selectedDrop = this;
inputIsSelected = false;
}

if(!inputIsSelected){
input.focus();
inputIsSelected=true;
}else{
inputIsSelected=false;
input.blur()
}





}
}
}
.datadrop {
padding: 7.5px;
width: 85px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}

.datadrop .drop {
width: 150px;
position: fixed;
display: none;
}

.datadrop .item {
padding: 10px;
}

.datadrop .item:hover {
background-color: #F1F3F4;
}

.datadrop .divider {
padding: 5px;
color: grey;
}

.datadrop .divider:hover {
background-color: #ffffff;
}

.datadrop input {
width: 60px;
color: black;
}

.datadrop:hover {
background-color: #F1F3F4;
color: #5F6368;
}

.datadrop .click {
color: #5F6368;
}

.datadrop input {
background-color: transparent;
border: 1px solid transparent;
outline: none;
padding: 5px;
}

.datadrop input:focus {
background-color: white;
border: 1px solid black;
border-radius: 2.5px;
}
<div datadrop="scale" data-input="scales-1" class="datadrop">
<input type="text" id="scales-1" value="100%"> &nbsp;&nbsp;|&nbsp; <i class="fa-solid fa-caret-down click"></i>
<div class="drop" id="scale">
<div class="item">Fit</div>
<div class="item divider">
<hr>
</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
</div>
</div>
<div datadrop="scale" data-input="scales-2" class="datadrop">
<input type="text" id="scales-2" value="100%"> &nbsp;&nbsp;|&nbsp; <i class="fa-solid fa-caret-down click"></i>
<div class="drop" id="scale">
<div class="item">Fit</div>
<div class="item divider">
<hr>
</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
<div class="item">Fit</div>
</div>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />

关于javascript - 如何仅使用 JavaScript 切换输入的焦点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71869760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com