window.onblur event also triggers window.onfocus event when using IE8

有些话、适合烂在心里 提交于 2019-12-23 03:18:37

问题


Both the events are getting fired at the time onblur event.

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"  CodeBehind="WebForm1.aspx.cs"
Inherits="WebApplication1.WebForm1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="js/jquery-1.7.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        window.onfocus = function() {
            $('#msg').html($('#msg').html() + '<br/> focus');
        };
        window.onblur = function() {
            $('#msg').html($('#msg').html() + '<br/> Blur');
        };
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">   
    <div id="msg">
    </div>
</asp:Content>

回答1:


Finally it got solved. Thank you for all the help!

$(window).bind("load", function() {
        pageLoad();
    });

    var activeElement;

    function pageLoad() {
        activeElement = document.activeElement;
        document.onfocusout = logWindow;
    }

    function logWindow() {
        if (activeElement != document.activeElement) {
            activeElement = document.activeElement;
        }
        else {
            $('#msg').html($('#msg').html()+'<br/> Focus');
        }

    }



回答2:


 window.onblur = function() { 
      $('#msg').html($('#msg').html() + '<br/> Blur');     
      return false;
 }; 

May be the event is getting bubbled up.



来源:https://stackoverflow.com/questions/10929432/window-onblur-event-also-triggers-window-onfocus-event-when-using-ie8

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