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

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


Giới thiệu

Thuộc tính type (read-only) của DOM Input Button trả về loại nút: "button", "submit", hoặc "reset". Không thể thay đổi qua DOM (chỉ đọc).

Cú pháp

button.type

Ví dụ minh họa

<!DOCTYPE html>
<html>
<head>
  <style>
    .btn { display:block; margin:1rem auto; background:#db133a; color:#fff; border:1px solid #db133a; padding:0.5rem; border-radius:50px; width:40%; }
    .show-type { font-weight:bold; font-size:1.4rem; color:#ffc107; }
  </style>
</head>
<body>
  <h1>Ví dụ type Property</h1>
  <input type="submit" onclick="getType()" class="btn" value="Nhấn để biết type của tôi">
  <div class="show-type"></div>
  <script>
    function getType() {
      var btnType = document.querySelector(".btn").type;
      document.querySelector(".show-type").innerHTML = btnType;
    }
  </script>
</body>
</html>

Kết quả hiển thị

Nhấp nút hiển thị "submit" (loại nút hiện tại).