2011-10-18 26 views
6

Come impostare lo stato attivo su un elemento html?jquery focus on load

Io uso document.getElementById('ID_HTML_wanted').focus(); ma il mio elemento html "ID_HTML_wanted" non è lo stato attivo. Mi utente jquery api .focus

+1

stupido, ho omettere impostare fuoco quando DOM è pronto : $ (document) .ready (function() { document.getElementById ('ID_HTML_wanted'). Focus(); }); – immobiluser

risposta

10

Cercate di avvolgere il codice per il codice in modo che viene eseguito dopo DOM è pronto

$(function(){ 
    //your code 
}); 

in modo che diventi

$(function(){ 
    document.getElementById('ID_HTML_wanted').focus(); 
}); 

Tuttavia, il vostro element't don' t avere il metodo .focus(), se si desidera REALMENTE utilizzare quello di jQuery, utilizzare

$(function(){ 
    $("#ID_HTML_wanted").focus(); 
}); 
+0

Sì, sono stupido, ho omesso di mettere a fuoco quando DOM è pronto: '$ (document) .ready (function() {$ (" # ID_HTML_wanted "). Focus();});' – immobiluser

2

Sor ry, ho efficacemente omettere impostare fuoco quando DOM è pronto:

$(document).ready(function() { 
    $("#ID_HTML_wanted").focus(); 
}); 

Tutti e tre i seguenti sintassi di .ready() sono equivalenti:

$(document).ready(handler) 
$().ready(handler) (this is not recommended) 
$(handler)