Here’s a more robust answer than the currently accepted one:
jQuery.expr[':'].focus = function(elem) {
return elem === document.activeElement && (elem.type || elem.href);
};
Note that the (elem.type || elem.href)
test was added to filter out false positives like body
. This way, we make sure to filter out all elements except form controls and hyperlinks.