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

Thuộc tính autocomplete của HTML DOM Input URL

Thuộc tính autocomplete của đối tượng Input URL trong HTML DOM bật/tắt tính năng gợi ý tự động hoàn thành dựa trên lịch sử nhập trước đó của người dùng. Giúp tăng tốc độ nhập URL thường dùng.

Cú pháp

Lấy giá trị

inputURLObject.autocomplete

Thiết lập autocomplete

inputURLObject.autocomplete = 'giá_trị'

Các giá trị

Giá trịChi tiết
onBật autocomplete (mặc định của trình duyệt).
offTắt autocomplete, không gợi ý lịch sử.

Ví dụ minh họa

Ô URL có autocomplete="off" ban đầu. Nhấn nút để bật gợi ý:

<!DOCTYPE html>
<html>
<head>
<title>Input URL autocomplete</title>
<style>
  form { width: 70%; margin: 0 auto; text-align: center; }
  * { padding: 2px; margin: 5px; }
  input[type="button"] { border-radius: 10px; }
</style>
</head>
</head>
<body>
<form>
  <fieldset>
    <legend>URL-autocomplete</legend>
    <label for="URLSelect">URL: <input type="url" id="URLSelect" placeholder="vd: https://www.xyz.com/" autocomplete="off" autofocus> </label> 
    <input type="button" onclick="batGoiY()" value="Bật gợi ý"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputURL = document.getElementById("URLSelect"); 
  divDisplay.textContent = 'Gợi ý: ' + inputURL.autocomplete; 
  function batGoiY() { 
    inputURL.autocomplete = 'on'; 
    divDisplay.textContent = 'Gợi ý: ' + inputURL.autocomplete; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Ban đầu: "Gợi ý: off", trình duyệt không hiện danh sách URL đã truy cập.
  • Nhấn Bật gợi ý: "Gợi ý: on", trình duyệt sẽ gợi ý các URL đã nhập trước đó.

Lưu ý bảo mật: Trên các trang nhạy cảm (ngân hàng, đăng nhập), nên đặt autocomplete="off" để tránh rò rỉ thông tin trên máy chia sẻ.