Action on blur except when specific element clicked with jQuery

前端 未结 5 1064
旧时难觅i
旧时难觅i 2021-01-01 09:33

There are two elements in play:

$(\'#myInput\') // an input field for search
$(\'#myList\') // a list to display search results

I want to h

5条回答
  •  被撕碎了的回忆
    2021-01-01 09:46

    You need to be able to say "do this blur() unless the list gains focus at the same time".

    This question says how to detect if an element has focus: Using jQuery to test if an input has focus

    Then all you need to do is:

    $("#myInput").blur(function () {
        if (!$("#myList").is(":focus")) {
            $("#myList").hide();
        }
    });
    

提交回复
热议问题