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

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

Thuộc tính autofocus của HTML DOM Input Color thiết lập/trả về xem Input Color có được focus tự động khi tải trang ban đầu hay không.

Cú pháp

Cú pháp như sau:

  • Trả về giá trị boolean - true/false:
inputColorObject.autofocus
  • Thiết lập autofocus thành booleanValue:
inputColorObject.autofocus = booleanValue

Giá trị boolean

Ở đây, 「booleanValue」 có thể là các giá trị sau:

booleanValueChi tiết
trueĐịnh nghĩa rằng input sẽ được autofocus khi tải trang.
falseLà giá trị mặc định và input không được autofocus.

Ví dụ

Dưới đây là ví dụ về thuộc tính Input Color autofocus:

<!DOCTYPE html> <html> <head> <title>Input Color Autofocus</title> </head> <body> Bộ chọn màu: <input type="color" id="Color" autofocus> <button onclick="removeAutoFocus()">Gỡ bỏ Auto Focus</button> <div id="divDisplay"></div> <script>    var divDisplay = document.getElementById("divDisplay");    var inputColor = document.getElementById("Color");    divDisplay.textContent = 'Autofocus: '+inputColor.autofocus function removeAutoFocus() {       if(inputColor.autofocus == true){          inputColor.autofocus = false;          divDisplay.textContent = 'Autofocus: '+inputColor.autofocus       }    } </script> </body> </html>

Kết quả

Đoạn mã trên sẽ cho kết quả như sau:

Trước khi nhấn 『Gỡ bỏ Auto Focus』:

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

Sau khi nhấn 『Gỡ bỏ Auto Focus』:

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