简体   繁体   English

按钮必须出现在输入焦点上,但在单击表单的另一个按钮时会消失

[英]Button must appear on input focus, but it disappears while clicking another button of the form

I have a problem with a button that appears when an input have focus.当输入具有焦点时出现的按钮有问题。 "Cancel" button must be visible when a user interact with a search bar.当用户与搜索栏交互时,“取消”按钮必须可见。 I've made the job done with addEventListener on input click/focus.我已经在输入点击/焦点上使用 addEventListener 完成了工作。 But there is a problem, when I click "clear" button my "cancel" button disappears on mobile, cause input loses focus.但是有一个问题,当我点击“清除”按钮时,我的“取消”按钮在移动设备上消失了,导致输入失去焦点。 Is there a way to trigger button's appearing on another event suited for my goal?有没有办法触发按钮出现在适合我目标的另一个事件上? Or may be there is a method to keep focus on input while pressing the "clear" button.或者可能有一种方法可以在按下“清除”按钮时将注意力集中在输入上。 Thanks!谢谢!

I've tried:我试过了:

  • mousedown events, mousedown 事件,
  • addEventListenerAll on all elements of the from, addEventListenerAll 在 from 的所有元素上,
  • changing click to focus and other..改变点击焦点和其他..

My html我的 html

<form class="search__form">
<button class="search__cancel" data-search-cancel-appears 
tabindex="0">cancel</button>
<input class="search__input" data-search-cancel-appears type="search">
<button class="search__clear" data-search-cancel-appears tabindex="0">clear</button>
<button class="search__submit" data-search-cancel-appears 
type="submit">search</button>
</form>

my css我的 css

/* clears the 'X' from Internet Explorer */
input[type=search]::-ms-clear {  display: none; width : 0; height: 0; }
input[type=search]::-ms-reveal {  display: none; width : 0; height: 0; }

/* clears the 'X' from Chrome */
input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-results-button,
input[type="search"]::-webkit-search-results-decoration { display: none; }

my JS我的 JS

const searchCancel = document.querySelector('.search__cancel')
const searchClear = document.querySelector('.search__clear')
const searchInput = document.querySelector('.search__input')

searchCancel.style.display = "none"
searchClear.style.display = "none"


searchClear.addEventListener('click', function (e) {
e.preventDefault()
searchInput.value = ""
searchInput.focus()
searchCancel.style.display = ""
searchClear.style.display = "none"
})

searchCancel.addEventListener('click', function (e) {
e.preventDefault()
searchInput.value = ""
searchCancel.click()
searchCancel.style.display = "none"
searchClear.style.display = "none"
})

searchInput.addEventListener('input', function (e) {
searchCancel.style.display = ""
if (searchInput.value.length > 0) {
  searchClear.style.display = ""
} else {
  searchClear.style.display = "none"
}
})

searchInput.addEventListener('click', function (e) {
searchCancel.style.display = ""
if (searchInput.value.length > 0) {
  searchClear.style.display = ""
} else {
  searchClear.style.display = "none"
}
})

searchInput.addEventListener('focusout', function (e) {
console.log(e.relatedTarget)
setTimeout(() => {
if (e.relatedTarget == searchClear) {
  console.log(1)
} else {
  searchCancel.style.display = "none"
}
}, 500)
})


searchInput.addEventListener("keyup", function (event) {
if (event.keyCode === 46) {
event.preventDefault();
if (searchInput.value.length == 0) {
  searchClear.style.display = "none"
}
}
});

Do you necessarily need to hide the cancel button on focus lost?您是否一定需要在焦点丢失时隐藏取消按钮?

How about hiding it only if the input doesn't have any value, something like this:仅当输入没有任何值时才隐藏它如何,如下所示:

searchInput.addEventListener('focusout', function (e) {
  if (!searchInput.value) { // trim if needed
    searchCancel.style.display = "none"
  }
});

This way even if you click the clear button (triggering focusout) it will not hide the cancel button because at that point the input is not going to be empty.这样,即使您单击清除按钮(触发聚焦),它也不会隐藏取消按钮,因为此时输入不会为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM