Make a search menu disappear when the search field loses focus, but still allow user to click on links

无人久伴 提交于 2020-01-02 04:29:11

问题


On my website I have a search box (text input field). When the user clicks on it and starts typing text a menu of links appears. The menu appears via JQuery - The following command makes the menu appear:

".focus(function() {
    $("#instant_search_wrapper").show();
}); "

When the user clicks off the search box, I would like the menu to disappear.

The easiest way of doing this would be to be use the following command:

".blur(function() {
    $("#instant_search_wrapper").hide();
});" 

However, if I do this, then when the user clicks on a link in the menu, the text input field loses focus and so the menu disappears before the user is taken to the select page. How can I make it so the menu disappears when the search field loses focus, (but if the user clicks on a link before the search field loses focus, s/he is still able to be taken to the link)?


回答1:


You need to bind the click to body for hiding

$(document).click(function() {
      $("#instant_search_wrapper").hide();

});

See Working example at http://jsfiddle.net/WYbp3/4/




回答2:


Can you be more specific, I don't understand the question. I checked your site and when I start typing something, a modal box style menu with the results is showed. Then if I blur the input the box still there.



来源:https://stackoverflow.com/questions/5197712/make-a-search-menu-disappear-when-the-search-field-loses-focus-but-still-allow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!