Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính autofocus của thẻ HTML <input>


Thuộc tính autofocus của phần tử <input> được dùng để chỉ định ô <input> nào sẽ nhận focus (tiêu điểm) khi trang web tải xong. Thuộc tính này được giới thiệu trong HTML5.

Cú pháp:

<input autofocus>

Ví dụ

<!DOCTYPE html> <html> <body> <h2>Đăng nhập tài khoản</h2> <form action="" method="get">    ID: <input type="text" name="id"><br>    Mật khẩu: <input type="password" name="pwd"><br>    Ngày sinh: <input type="date" name="dob" autofocus><br>    <button type="submit" value="Submit">Gửi</button> </form> </body> </html>

Kết quả

Thuộc tính autofocus của thẻ HTML <input>

Trong ví dụ trên, chúng ta đã tạo một form:

<form action="" method="get">    ID: <input type="text" name="id"><br>    Mật khẩu: <input type="password" name="pwd"><br>    Ngày sinh: <input type="date" name="dob" autofocus><br>    <button type="submit" value="Submit">Gửi</button> </form>

Chúng ta đã đặt autofocus cho ô nhập thứ ba là Ngày sinh, do đó con trỏ sẽ hiển thị ngay tại đó:

Ngày sinh: <input type="date" name="dob" autofocus><br>