如何检测用户是否单击了我的 html 页面所在的页面选项卡?
我正在开发一个网络聊天,当对话发生变化时,我需要引发一个事件,以便页面标题发生变化,以便一个用户可以知道另一个用户已经编写了任何内容。
所以我尝试了代码:
addEvent(window,'focus',function(){ alert(1); } );
和 addEvent(文档,'焦点',function(){ 警报(2); } );
但它不起作用。
即使用户没有点击网页,我也需要引发该事件。
对于那个事件,我已经有了解决方案:
<body onclick="showClickedElement(this);" >
有什么解决方案吗?
提前致谢。
I'm developing a web chat and I need to raise an event when the conversation has changed so the page title changes so one user can know that the other user has written anything.
So I tried the code:
addEvent(window,'focus',function(){ alert(1); } );
and
addEvent(document,'focus',function(){ alert(2); } );
but it does not work.
I need the event to be raised even if the user does not click on the web page.
For that event, I've got the solutions:
<body onclick="showClickedElement(this);" >
Is there any solution?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取自: http://bob.pythonmac.org/ archives/2010/04/25/tab-visible-event/
如果它不适用于您的 addEvent,您可能需要尝试 jQuery(请参阅 用户切换浏览器选项卡时的事件)
taken from: http://bob.pythonmac.org/archives/2010/04/25/tab-visible-event/
If it doesn't work with your addEvent, you might want to try jQuery (see Event for when user switches browser tabs)
我发现这段代码适用于 IE6、7,8 和 firefox,不需要 Jquery。
};
函数 onFocus(){
document.body.className = '重点';
};
if (/@cc_on!@/false) { // 检查 Internet Explorer
document.onfocusin = onFocus;
document.onfocusout = onBlur;
} 别的 {
window.onfocus = onFocus;
window.onblur = onBlur;
来源
I've found this code that works for IE6, 7,8 and firefox and no not need Jquery.
};
function onFocus(){
document.body.className = 'focused';
};
if (/@cc_on!@/false) { // check for Internet Explorer
document.onfocusin = onFocus;
document.onfocusout = onBlur;
} else {
window.onfocus = onFocus;
window.onblur = onBlur;
}
source