In web browsers, what's the difference between onblur and onfocusout?

前端 未结 5 1506
一向
一向 2020-12-02 21:59

If they\'re the same, then why there are two of this kind of event?

相关标签:
5条回答
  • 2020-12-02 22:20

    The Jquery documentation has a good focusout vs. blur demo which I'll reproduce below for clarity. In short, focusout fires if the selector — $('p') in the demo — is anything including the inputs and parent elements. Whereas, blur only fires if the selector is on the inputs — $('input').

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>focusout demo</title>
      <style>
      .inputs {
        float: left;
        margin-right: 1em;
      }
      .inputs p {
        margin-top: 0;
      }
      </style>
      <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
    
    <div class="inputs">
      <p>
        <input type="text"><br>
        <input type="text">
      </p>
      <p>
        <input type="password">
      </p>
    </div>
    <div id="focus-count">focusout fire</div>
    <div id="blur-count">blur fire</div>
    
    <script>
    var focus = 0,
      blur = 0;
    $( "p" )
      .focusout(function() {
        focus++;
        $( "#focus-count" ).text( "focusout fired: " + focus + "x" );
      })
      .blur(function() {
        blur++;
        $( "#blur-count" ).text( "blur fired: " + blur + "x" );
      });
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-02 22:23

    There's essentially no difference in 2017:

    https://www.quirksmode.org/js/events_order.html

    Few web developers consciously use event capturing or bubbling. In Web pages as they are made today, it is simply not necessary to let a bubbling event be handled by several different event handlers. Users might get confused by several things happening after one mouse click, and usually you want to keep your event handling scripts separated.

    0 讨论(0)
  • 2020-12-02 22:28

    The focusout event fires when an element is about to lose focus.

    The main difference between this event and blur is that focusout bubbles while blur does not. Most times, they can be used interchangeably.

    0 讨论(0)
  • 2020-12-02 22:31

    Acccording to the spec for the focusout event type:

    This event type is similar to blur, but is dispatched before focus is shifted, and does bubble.

    Whereas blur events do bubble, and are dispatched later.

    0 讨论(0)
  • 2020-12-02 22:46

    As you know, the onBlur event fires for an element if that element had the focus, but loses it.

    The onFocusOut event fires in this case, but also triggers if any child element loses focus.

    For example, you have a div with special formatting because the human is currently editing a field in that area. You'd could use onFocusOut to turn that formatting off when focus leaves that div.

    Up until very recently, onFocusOut was only used by IE. If that has changed, it has been very recent. Test in FF, Chrome, etc.

    0 讨论(0)
提交回复
热议问题