Tôi đang cố đặt trọng tâm mặc định vào hộp nhập khi tải trang (ví dụ: google). Trang của tôi rất đơn giản, nhưng tôi không thể tìm ra cách để làm điều này.Đặt tiêu điểm vào hộp nhập HTML khi tải trang
Đây là những gì tôi đã có cho đến nay:
<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>
Tại sao điều đó không làm việc trong khi điều này hoạt động tốt?
<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>
Trợ giúp được nhiều đánh giá cao :-)
“hoạt động trong tất cả các trình duyệt hiện nay” - đó thực sự phụ thuộc vào định nghĩa của bạn về “tất cả các trình duyệt hiện tại” –
ie9: http://stackoverflow.com/questions/8280988/how-to-make-input-autofocus-in-internet-explorer – jedierikb
@BhaveshGangani ['autofocus' là không được hỗ trợ trong Internet Explorer 9 và các phiên bản cũ hơn.] (http://www.w3schools.com/tags/att_input_autofocus.asp) –