0

如果用户单击页面外,是否可能触发事件?更具体地说,在浏览器标题栏或标签栏中?

试试这个我的jsFiddle 示例

HTML

<div id="box">
    <div id="menu">Click here</div>
    <div id="show">Hello!</div>
</div>

JS

jQuery('div#menu').on('click', function(ev) {
    jQuery('div#show').addClass('visible').css({
        top: ev.clientY,
        left: ev.clientX
    });

    return false;
});

jQuery(document).click(function() {
    jQuery('div#show').removeClass('visible');
});

jQuery(window).blur(function() {
    jQuery(document).click();
});

测试

首先,您单击“单击此处”按钮。所以...

  1. 尝试单击某些文档部分:确定
  2. 尝试单击某些页面部分或取消焦点浏览器窗口:好的
  3. 尝试在浏览器中单击其他打开的选项卡:好的
  4. 尝试点击浏览器标题栏或标签标题:失败

所以...

我有可能解决这个问题吗?

测试结果

  • Chrome:最后失败;
  • Firefox:最后失败;
  • IE10:最后失败;
  • Opera:最后失败;
4

1 回答 1

1

很抱歉告诉你这个,但这是不可能的。

Reason? Well essentially, clicking the title bar/tab bar of a window doesn't actually make an element lose focus.

Try this example here:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onblur

You'll notice that clicking the title bar/tab bar doesn't make the textbox lose focus at all, which is the root of the issue.

I don't think this is a big deal for your project though. As far as the user's concerned, it'll look great as it currently is.

于 2013-03-02T07:32:52.577 回答