onfocusout / onfocusin not working
onfocusout / onfocusin not working
我刚刚了解了onfocusout
、onfocusin
,但这些函数似乎不起作用。我用firefox和chrome都试过了。根本没有控制台消息,即使我有焦点并放松了它。我做错了什么?
window.addEventListener
(
"onfocusout",
function()
{
console.log("page lost focus");
}
);
window.addEventListener
(
"onfocusin",
function()
{
console.log("page has focus");
}
);
<h1>Test</h1>
使用addEventListener,您不会将"on"与事件名称一起使用
function example(evt) {
console.log(evt.type, event.target.id);
}
window.addEventListener("focusin", example);
window.addEventListener("focusout", example);
<input type="text" id="foo" />
<input type="text" id="bar" />
如果您想知道页面状态,那么您需要查看不同的事件:pageshow
和pagehide
我对此做了一些研究。只有当你真正选择一个文本字段时,你才能获得"焦点"。来源:w3schools
那么,当你最小化屏幕时,你想要的是控制台记录一些东西吗?还是选择另一个窗口?如果是这样的话,我在这里找到了一篇关于的帖子
只需使用以下代码片段,它更容易阅读,但与事件处理程序相同:-)
$(function(){
$('.yourfocuselement').focus(function(){
//The code for element focus goes here
})
$('.yourfocuselement').blur(function(){
//The code for element lose focus goes here
})
})
此外,我可能不知道你从这段代码中的全部要求,但我不明白为什么你需要为页面焦点添加一个监听器。。。
相关文章:
- $rootScope not working
- jQuery document.ready not working
- Javascript getElementsByTagName not working?
- JQuery.val( ) not working
- Tomcat websocket is not working
- Javascript JSON.parse not working
- NodeJS Multer is not working
- Regex not working
- Ember run.debounce not working
- AngularJs 1 not working
- document.getElementById.style.backgroundImage not working
- Rxjs5, distinct() is not working
- createOscillator noteOn not working
- Javascript - .getHours() not working
- Javascript setTimeout not working | onkeydown
- MongoDB $pull not working
- Express router.use not working
- JSONP not working
- window.opener not working in chrome & IE
- Backbone + RequireJS: Collection.each() is not working