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

Thuộc tính autofocus của HTML DOM Input Time

Thuộc tính autofocus của đối tượng Input Time trong HTML DOM xác định ô nhập giờ có tự động nhận focus khi trang web tải xong hay không. Chỉ một phần tử trên trang được autofocus.

Cú pháp

Lấy giá trị boolean

inputTimeObject.autofocus

Thiết lập autofocus

inputTimeObject.autofocus = booleanValue

Các giá trị boolean

booleanValueChi tiết
trueÔ time tự động focus khi tải trang.
falseMặc định: không tự động focus.

Ví dụ minh họa

Ô time có autofocus=true. Nhấn nút để tắt autofocus (chỉ ảnh hưởng reload sau):

<!DOCTYPE html>
<html>
<head>
<title>Input Time autofocus</title>
<style>
  form { width: 70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin: 5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
<form>
  <fieldset>
    <legend>Time-autofocus</legend>
    <label for="TimeSelect">Giờ thi: <input type="time" id="TimeSelect" value="10:00" autofocus> </label> 
    <input type="button" onclick="xoaAutofocus()" value="Tắt Autofocus"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputTime = document.getElementById("TimeSelect"); 
  divDisplay.textContent = 'Autofocus: ' + inputTime.autofocus; 
  function xoaAutofocus() { 
    inputTime.autofocus = false; 
    divDisplay.textContent = 'Autofocus: ' + inputTime.autofocus; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Tải trang: ô time 10:00 tự động focus (sẵn sàng nhập).
  • Hiển thị "Autofocus: true".
  • Nhấn Tắt Autofocus: hiển thị "Autofocus: false" (hiệu lực khi tải lại trang).

Lưu ý: Thay đổi autofocus qua JS chỉ ảnh hưởng đến lần tải trang sau. Để focus ngay lập tức, dùng inputTime.focus().