简体   繁体   English

在外部单击时如何隐藏输入?

[英]How to hide input when you click outside of it?

I have a use case where a user clicks on a search icon, and it display an input. 我有一个用例,其中用户单击搜索图标,并显示输入。

I want to make it so that when the input is open, anytime you click outside of the input, it hides it. 我想这样做,以便在打开输入时,只要您在输入外部单击,它就会将其隐藏。

Of course, I want to keep the toggle I have in place currently as well. 当然,我也想保持当前的切换状态。

Trying to figure out a solution in vanilla JavaScript, not jQuery. 尝试找出使用普通JavaScript(而非jQuery)的解决方案。

 ///////////// Nav - Search Functionality // Toggle showing and not showing the input const searchIcon = document.getElementById('search-icon'); const searchInput = document.getElementById('search-input'); searchIcon.addEventListener('click', function() { searchInput.classList.toggle('is-active'); searchInput.classList.toggle('transform-is-active'); }); 
 .is-active { display: block !important; } .is-hidden { display: none !important; } .transform-is-active { width: 200px !important; } #search-input { display: none; margin-right: 10px; transition: all 2s ease; } #search-icon { cursor: pointer; } 
 <!--Search--> <input id="search-input" class="input" type="text" placeholder="Search..."> <img id="search-icon" src="https://static.thenounproject.com/png/101791-200.png" alt="Search" tabindex="0" /> 

I'd use the blur event to capture this: 我会使用blur事件捕获此事件:

const searchIcon = document.getElementById('search-icon');
const searchInput = document.getElementById('search-input');

searchIcon.addEventListener('click', function(event) {
  searchInput.classList.toggle('is-active');
  searchInput.classList.toggle('transform-is-active');
  event.stopPropagation();
});

searchInput.addEventListener('blur', function(event) {
  searchInput.classList.remove('is-active');
  searchInput.classList.remove('transform-is-active');
});

More details here: https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event 此处有更多详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/Element/blur_event

Try this, i hope it'll help you out. 试试这个,希望对您有所帮助。 Thanks 谢谢

 ///////////// Nav - Search Functionality // Toggle showing and not showing the input const searchIcon = document.getElementById('search-icon'); const searchInput = document.getElementById('search-input'); searchIcon.addEventListener('click', function() { searchInput.classList.toggle('is-active'); searchInput.classList.toggle('transform-is-active'); event.stopPropagation(); }); window.addEventListener('click',function(e){ if(e.target != document.querySelector('div.a')){ searchInput.classList.remove('is-active'); } }); 
 .is-active { display: block !important; } .is-hidden { display: none !important; } .transform-is-active { width: 200px !important; } #search-input { display: none; margin-right: 10px; transition: all 2s ease; position: relative; z-index: 999999; } #search-icon { cursor: pointer; } 
 <!--Search--> <input id="search-input" class="input" type="text" placeholder="Search..."> <img id="search-icon" src="https://static.thenounproject.com/png/101791-200.png" alt="Search" tabindex="0" /> 

There you go. 妳去 It hide the input when you click the body (outside the icon) and the toggle is preserved. 单击主体时(在图标外部),它将隐藏输入,并保留切换。

const searchIcon = document.getElementById('search-icon');
const searchInput = document.getElementById('search-input');

searchInput.addEventListener('click', function(event){
  event.stopPropagation();
});

searchIcon.addEventListener('click', function(event) {
  searchInput.classList.toggle('is-active');
  searchInput.classList.toggle('transform-is-active');
  event.stopPropagation();
});

document.body.addEventListener('click', function(event) {
  searchInput.classList.remove('is-active');
  searchInput.classList.remove('transform-is-active');
  event.preventDefault();
});

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

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