Action on blur except when specific element clicked with jQuery

前端 未结 5 1079
旧时难觅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 10:00

    You can accomplish this by keeping a global variable, and setTimouts, to wait a delay of 200ms and then check if one of the 2 elements have focus.

    var keepFocus = false;
    
    function hideList(){
        if(!keepFocus){
            $('#myList').hide();
        }
    }
    
    $('#myInput').blur(function() {
        keepFocus = false;
        window.setTimeout(hideList, 200);
    }).focus(function(){
        keepFocus = true;
    });
    
    
    $('#myList').blur(function() {
        keepFocus = false;
        window.setTimeout(hideList, 200);
    }).focus(function(){
        keepFocus = true;
    });
    

提交回复
热议问题