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();
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;
});
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.
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.
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);
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.
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");
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")) {
...
}
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")
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