为了检测焦点的丢失,对于可访问性审计,我个人使用以下所有丑陋的组合:
(function($){
var focusCounter = 0;
/* Set focus outline (CSS, jQuery): */
$("*").on("focus","body",function(e){$(e.target).css("outline","5px solid orange");$(e.target).on("blur",function(e){$(e.target).css("outline","none");})});
/* Log keypress */
$(document).on('keydown',function(e){
console.log("========= KEYDOWN ", e.key, " ==================");
});
/* Log currently focused element (On start) */
console.log("CURRENT: ",document.activeElement);
/* Log focused element (On focus) */
document.addEventListener("focus",function(){
console.log(
"[ " + (focusCounter++).toString() + " ]",
"FOCUSED: ",
"<" + document.activeElement.nodeName + ">",
($.trim($(document.activeElement).text())? $.trim($(document.activeElement).text()) : $.trim($(document.activeElement).val())).replace(/(\r\n|\n|\r)/gm, "").substring(0, 30) + '...',
document.activeElement
);
},true);
/* Try to detect loss of focus (Works in Chrome) */
document.addEventListener("focusout", function(event){console.log((!event.relatedTarget)?"⚠ FOCUS LOST":"");});
/* Fallback, checks in intervals if the focus is still active/lost e.g. if onfocus or onfocusout didn't work */
(function(){setInterval(function(){console.log('interval check: ',(document.body===document.activeElement)?"FOCUS LOST":"ok");},8500);})();
})(jQuery);
想法:我将所有这些设置为书签,以便我可以轻松访问它