2011-10-18 13 views
6

Jak ustawić fokus na elemencie HTML?jquery focus on load

Używam document.getElementById('ID_HTML_wanted').focus();, ale mój element html "ID_HTML_wanted" jako nie fokus. I użytkownik jquery api .focus

+1

Głupi, mam pominąć, aby ustawić ostrość, gdy DOM jest gotowy : $ (dokument) .ready (function() { document.getElementById ('ID_HTML_wanted'). Focus(); }); – immobiluser

Odpowiedz

10

Spróbuj owinąć swój kod do tego kodu, więc wykonuje PO DOM jest gotowy

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

więc będzie stać

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

jednak swój element't don” t mieć metodę .focus(), jeśli naprawdę chcesz użyć jQuery jeden, użyj

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

Tak, jestem głupi, nie mam do ustawiania ostrości, gdy DOM jest gotowy: '$ (dokument) .ready (function() {$ (" # ID_HTML_ chciany "). Focus();});' – immobiluser

2

Sor ry, mam skutecznie pominąć, aby ustawić ostrość, gdy DOM jest gotowy:

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

Wszystkie trzy z poniższych składni z .ready() są równoważne:

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