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

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

Thuộc tính type của đối tượng Input Email trả về hoặc thiết lập kiểu của ô nhập liệu. Mặc định là email, nhưng có thể thay đổi thành các kiểu khác như datetime-local, radio, tel thông qua JavaScript.

Cú pháp

Lấy giá trị type

inputEmailObject.type

Thiết lập type

inputEmailObject.type = 'giá_trị_chuỗi'

Các giá trị chuỗi

stringValueChi tiết
emailÔ nhập email (mặc định).
datetime-localÔ chọn ngày giờ cục bộ (không múi giờ).
radioNút radio chọn một.
telÔ nhập số điện thoại (bàn phím số trên mobile).

Ví dụ minh họa

Ví dụ hiển thị kiểu ô nhập liệu hiện tại (viết hoa) khi nhấn nút:

<!DOCTYPE html>
<html>
<head>
<title>Input Email type</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-type</legend>
    <label for="EmailSelect"></label> 
    <input type="email" id="EmailSelect" > 
    <input type="button" onclick="getTypeOfInput()" value="Nhập gì?"> 
  </fieldset> 
</form> 
<script> 
  var labelSelect = document.querySelector("label"); 
  var inputEmail = document.getElementById("EmailSelect"); 
  function getTypeOfInput() { 
    labelSelect.innerHTML = inputEmail.type.toUpperCase() + ' ID: '; 
  } 
</script> 
</body>
</html>

Kết quả

  • Ban đầu: label trống.
  • Sau khi nhấn nút: label hiển thị "EMAIL ID:" (vì type mặc định là email).

Lưu ý: Tương tự Input Text, việc thay đổi type động qua JS không khuyến khích do vấn đề tương thích trình duyệt. Nên khai báo đúng type trong HTML ban đầu.