2010-08-01 12 views
80

Sayfa yüklendiğinde giriş kutusuna varsayılan odağı ayarlamaya çalışıyorum (örnek: google). Sayfam çok basit, ancak bunu nasıl yapacağımı anlayamıyorum. Bu gayet iyi çalışıyor ikenOdağı bir HTML giriş kutusuna odaklama sayfa yükleme

<html> 
<head> 
<title>Password Protected Page</title> 

<script type="text/javascript"> 
function FocusOnInput() 
{ 
document.getElementById("PasswordInput").focus(); 
} 
</script> 

<style type="text/css" media="screen"> 
    body, html {height: 100%; padding: 0px; margin: 0px;} 
    #outer {width: 100%; height: 100%; overflow: visible; padding: 0px; margin: 0px;} 
    #middle {vertical-align: middle} 
    #centered {width: 280px; margin-left: auto; margin-right: auto; text-align:center;} 
</style> 
</head> 
<body onload="FocusOnInput()"> 
<table id="outer" cellpadding="0" cellspacing="0"> 
    <tr><td id="middle"> 
    <div id="centered"> 
    <form action="verify.php" method="post"> 
    <input type="password" name="PasswordInput"/> 
    </form> 
    </div> 
    </td></tr> 
</table> 
</body> 
</html> 

Nasıl bu işe yaramazsa gelir:

Bu defa var nedir?

<input type="password" name="PasswordInput"/> 

şöyle bir id niteliği, sahip olmalıdır:

<html> 
<head> 
<script type="text/javascript"> 
function FocusOnInput() 
{ 
    document.getElementById("InputID").focus(); 
} 
</script> 
</head> 

<body onload="FocusOnInput()"> 
    <form> 
     <input type="text" id="InputID"> 
    </form> 
</body> 

</html> 

Yardım çok Bu hat

cevap

33

:-) takdir

<input type="password" name="PasswordInput" id="PasswordInput"/> 
271

Ve kullanabilirsiniz HTML5'in autofocus özniteliği (IE9 ve bel dışındaki tüm geçerli tarayıcılarda çalışır. ooo). Sadece komut dosyanızın IE9 veya daha eski bir sürümü veya diğer tarayıcıların eski bir sürümünü çağırması.

function focusOnInput() { 
    document.forms["passwordForm"]["passwordInput"].focus(); 
} 
+24

“tüm güncel tarayıcılarda çalışır” - yani gerçekten "tüm geçerli tarayıcılar" tanımınıza bağlıdır. –

+7

ie9: http://stackoverflow.com/questions/8280988/how-to-make-input-autofocus-in-internet-explorer – jedierikb

+0

@BhaveshGangani ['autofocus' Internet Explorer 9 ve önceki sürümlerinde desteklenmez.] (http://www.w3schools.com/tags/att_input_autofocus.asp) –

0

Ayrıca kullanabilirsiniz. Girdiye iki kez .focus() ekleyin.

Fix: -

function FocusOnInput() { 
    var element = document.getElementById('txtContactMobileNo'); 
    element.focus(); 
    setTimeout(function() { element.focus(); }, 1); 
} 

Ve üzerinde $ FocusOnInput() call (document) .ready (function() {.....};

2

Bu IE ile ortak konulardan biridir ve bu basittir için düzeltmek: JavaScript'inizin

<body onload="focusOnInput()"> 
    <form name="passwordForm" action="verify.php" method="post"> 
     <input name="passwordInput" type="password" /> 
    </form> 
</body> 

Ve sonra:

<input type="text" name="fname" autofocus> 
İlgili konular