Hide a DIV when it loses focus/blur

后端 未结 11 608
长情又很酷
长情又很酷 2021-02-05 08:34

I have a JavaScript that displays a DIV (sets its display css property from \'none\' to \'normal\'. Is there a way to give it focus as well so that when I click somewhere else o

相关标签:
11条回答
  • 2021-02-05 08:52

    Regarding mouse clicks, see the other answers.

    However regarding lost focus, .focusout is not the event to attach to, but rather .focusin. Why? Consider the following popup:

    <div class="popup">
      <input type="text" name="t1">
      <input type="text" name="t2">
    </div>
    

    What happens on moving from t1 to t2:

    • t1 sends focusout, which bubbles up to $('.popup').focusout
    • t2 sends focusin, which bubbles up to $('.popup').focusin

    ... so you get both types of event even though the focus stayed completely inside the popup.

    The solution is to analogous to the magic trick done with .click:

    $(document).ready(function() {
        $('html').focusin(function() {
            $('.popup').hide(); 
        });
        $('.popup').focusin(function(ev) {
            ev.stopPropagation(); 
        });
    });
    

    (side note: I found the .not(...) solution not working bc. of event bubbling).

    Bonus: working fiddle click me - open the popup, then try tabbing through the inputs.

    0 讨论(0)
  • 2021-02-05 08:52

    With jQuery you can hide elements with hide(), ex: $("#foo").hide()

    Hide element in event listener:

    $("#foo").blur(function() {
        $("#foo").hide();
    });
    
    0 讨论(0)
  • 2021-02-05 08:54

    I was also looking for this and here I found the solution https://api.jquery.com/mouseleave/. This may be useful for future readers.

    The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant.

    0 讨论(0)
  • 2021-02-05 08:58

    Showing is easy

    $('somewhere').click(function {$('#foo').show();})
    

    For hiding

    How do I hide a div when it loses its focus?

    0 讨论(0)
  • 2021-02-05 08:59

    The examples already given unfortunately do not work if you have an iframe on your site and then click inside the iframe. Attaching the event to the document will only attach it to same document that your element is in.

    You could also attach it to any iframes you're using, but most browsers won't let you do this if the iframe has loaded content from another domain.

    The best way to do this is to copy what's done in the jQuery UI menubar plugin.

    Basic example HTML:

    <div id="menu">Click here to show the menu
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li><a href="#">Item 3</a></li>
        </ul>
    </div>
    

    And the jQuery needed to make it work:

    var timeKeeper;
    
    $('#menu').click(function()
    {
        $('#menu ul').show();
    });
    
    $('#menu ul').click(function()
    {
        clearTimeout(timeKeeper);                  
    });
    
    $('#menu').focusout(function()
    {
        timeKeeper = setTimeout(function() {$('#menu ul').hide()}, 150);
    });
    
    $('#menu').attr('tabIndex', -1);
    $('#menu ul').hide();
    

    What it does is give the menu a tab index, so that it can be considered to have focus. Now that you've done that you can use the focusout event handler on the menu. This will fire whenever it has been considered to lose focus. Unfortunately, clicking some child elements will trigger the focusout event (example clicking links) so we need to disable hiding the menu if any child elements have been clicked.

    Because the focusout event gets called before the click event of any children, the way to achieve this is by setting a small timeout before hiding the element, and then a click on any child elements should clear this timeout, meaning the menu doesn't get hidden.

    Here is my working jsfiddle example

    0 讨论(0)
  • 2021-02-05 09:00
    $('.menu > li').click(function() {
        $(this).children('ul').stop().slideDown('fast',function()
        {
            $(document).one('click',function()
            {
                $('.menu > li').children('ul').stop().slideUp('fast');
            });
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题