Cómo seleccionar un elemento que tiene foco en él con jQuery


¿Cómo se puede seleccionar un elemento que tiene el foco actual?

No hay un filtro :focus en jQuery, por eso podemos usar algo como esto:

$('input:focus').someFunction();
Author: juan, 2009-02-05

8 answers

Realmente la mejor manera de hacerlo es configurar un controlador para el evento onFocus, y luego establecer una variable en el ID del elemento que tiene foco.

Algo como esto:

var id;

$(":input").focus(function () {
     id = this.id;
});
 32
Author: Mike_G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-06-11 04:53:20

$(document.activeElement) devolverá el elemento actualmente enfocado y es más rápido que usar el pseudo selector: focus.

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

 153
Author: Sam Shiles,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-01 15:17:31
alert($("*:focus").attr("id"));

Uso jQuery.

Alertará al id del elemento que se está enfocando.

Espero que sea útil para usted.

 123
Author: BiBi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-14 18:07:12

¿has probado

$.extend($.expr[':'], {
    focused: function(elem) { return elem.hasFocus; }
});

alert($('input :focused').length);
 5
Author: bendewey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-05 18:41:58

Si usa jQuery, puede escribir un selector como este :

$.expr[':'].focus = function(a){ return (a == document.activeElement); }

Luego puede seleccionar el elemento actualmente enfocado: $(":focus")

No solo los controles de formulario pueden tener foco. Cualquier elemento html con tabindex puede ser enfocado en los navegadores modernos.

 5
Author: jmanrubia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-01 15:18:25

Extraído de los ejemplos de la versión actual de jQuery (1.7) docs:

$(elem).is(":focus");
 4
Author: TurboHz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-08 09:29:14

Para comprobar el elemento tiene foco o no.

if ($("...").is(":focus")) {
  ...
}
 1
Author: Somnath Muluk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-09 09:57:05

Aquí está la versión CoffeeScript. Basado en el código de jmanrubia:

$.expr[':'].focus = (a) -> a is document.activeElement

También lo llamarías así $(".:focus")

 0
Author: Hengjie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-22 11:49:09