jQuery: How can I tell when a tab/window gains focus

戏子无情 提交于 2019-12-13 13:00:03

问题


Is there an event that I can register for that fires each time the user clicks on a tab, meaning that they were on my page's tab, clicked on another tab, then came back to my tab.

EDIT: By tabs, I mean browser tabs, not jQueryUI tabs.


回答1:


Matijis provided this answer in a comment, but I wanted to accept it so here it is as an answer.

$(window).on('focus', function() { // your code });



回答2:


Try using jquery $(selector).focusin or :focus, they both work quite the same way.

http://api.jquery.com/focusin/

http://api.jquery.com/focus-selector/



来源:https://stackoverflow.com/questions/9365576/jquery-how-can-i-tell-when-a-tab-window-gains-focus

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