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

Thuộc tính placeholder của HTML DOM Input Email

Thuộc tính placeholder của đối tượng Input Email đặt/gợi ý văn bản mẫu hiển thị trong ô email khi trống (màu mờ), giúp người dùng biết định dạng mong đợi.

Cú pháp

Lấy giá trị (chuỗi)

inputEmailObject.placeholder

Thiết lập giá trị

inputEmailObject.placeholder = stringValue

Ví dụ minh họa

Đặt placeholder gợi ý định dạng email nhân viên khi nhấn nút:

<!DOCTYPE html>
<html>
<head>
<title>Input Email placeholder</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>Email-placeholder</legend>
    <label for="EmailSelect">Mã nhân viên: <input type="email" id="EmailSelect"> </label>
    <input type="button" value="Nhập thông tin gì?" onclick="setPlaceholder()">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var inputEmail = document.getElementById("EmailSelect");
  function setPlaceholder() {
    inputEmail.placeholder = 'Ten-ID@MNC.com';
    divDisplay.textContent = inputEmail.placeholder + ' --> Thay Ten & ID bằng thông tin thực.';
  }
</script>
</body>
</html>

Kết quả

Ban đầu ô email trống không gợi ý. Nhấn nút đặt placeholder thành "Ten-ID@MNC.com" và hiển thị hướng dẫn thay thế.