Javascript onfocus / onblur 问题
所以我正在制作一个小搜索栏,在其下面显示结果,如下所示:
<div id='searchWrapper'>
<div id='Search'>
<input id="inputSpn" type="input" value="" />
<input id="searchBtn" type="image" src="/search.png" />
<br clear='all'/>
</div>
<div id='Results'>
</div>
</div>
我基本上是通过 AJAX 进行实时搜索。
最初是隐藏的,直到您键入并填充一些结果。然而,我的问题是当用户点击时让它隐藏。通过 onblur 使其消失也不起作用,因为我需要能够单击结果而不是让它们在我身上消失。不幸的是我还没有在互联网上找到好的非 jQuery 方法。最简单的表达方式是:如果单击
之外的任何内容,结果都会消失。谢谢!So I am making a little searchbar that shows results underneath of it like such:
<div id='searchWrapper'>
<div id='Search'>
<input id="inputSpn" type="input" value="" />
<input id="searchBtn" type="image" src="/search.png" />
<br clear='all'/>
</div>
<div id='Results'>
</div>
</div>
I am basically doing a livesearch with through AJAX. The <div id='Results'>
is initially hidden until you type and it populates some results. My issue however is getting it to hide when the user clicks away. having it disappear through onblur
does not work either as I need to be able to click on the results and not have them disappear on me. Unfortunately I have not been able to find a good non jQuery method on the internet yet. Simplest way to put it is: I want the results to disappear if anything but the <div id='searchWrapper'>
is clicked. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个未优化但有效的非 jQuery 解决方案
更新:
如果单击包装器 div 则切换
Here is a non-optimised but working non-jQuery solution
UPDATE:
Toggle if clicked on the wrapper div
向文档添加监听器(使用 addEventListener) - JSFiddle 示例
但是,如果您要在站点中使用大量 Javascript,您可能会考虑使用 jQuery - 它会让你的生活更轻松。
Add a listener to the document (using addEventListener) - JSFiddle Example
But if you are going to be using a significant amount of Javascript in your site, you might consider using jQuery - it will make your life much easier.