窗口模糊 - 警报框不断显示



我想在浏览器失去焦点时显示一个警告框。所以这是我的代码:

$(window).blur(function(e) {
    alert("Your browser lost focus");
    e.stopPropagation();
});

但是,警报框会不断弹出,例如当我使用 Alt+Tab 切换到另一个窗口时。谁能帮忙告诉我出了什么问题?

使用 $(window).blur - 一旦alert窗口弹出,alert本身就会导致您失去对浏览器的关注。强烈建议不要这样做。

如果你只想测试失去焦点,你应该使用:

 console.log("Your browser lost focus");

否则,不要向站点用户公开此信息 - 这将导致alert窗口的无限循环。

TRY THIS DEMO, ALERT ON LOST FOCUS, WHEN LOST FOCUS TRIGGER OR CALL ANY ACTION

试试这个小提琴

试试这个

$('selector').on(eventType, function (event) {
  alert(('cancelable' in event));   //will return true
  alert(event.cancelable);      //will return true if event can be cancelled
  //NOTE: Firefox mistakenly always returns true
});

最新更新