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

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

Thuộc tính type của <input type="month"> luôn trả về chuỗi "month". Thuộc tính này **chỉ đọc**.

Cú pháp

monthInput.type

Ví dụ: Kiểm tra type input month

<!DOCTYPE html>
<html>
<head>
    <style>
        html, body { height: 100%; margin: 0; font-family: sans-serif; }
        body {
            text-align: center; color: white;
            background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
        }
        input[type="month"] {
            display: block; width: 35%; margin: 15px auto;
            border: 2px solid white; background: transparent;
            color: white; font-weight: bold; padding: 8px;
        }
        .btn {
            background: #0197F6; border: none; color: white;
            padding: 10px 20px; border-radius: 4px; width: 35%;
            margin: 20px auto; display: block; cursor: pointer;
        }
        .show { font-size: 1.5rem; color: #dc3545; font-weight: bold; margin-top: 15px; }
    </style>
</head>
<body>
    <h1>Demo Input Month type Property</h1>
    <p>Bạn muốn biết type của tôi?</p>
    <input type="month" class="monthInput" required>
    <button onclick="getType()" class="btn">Lấy giá trị Type</button>
    <div id="result" class="show"></div>
    <script>
        function getType() {
            var input = document.querySelector(".monthInput");
            document.getElementById("result").textContent = "type = " + input.type;
        }
    </script>
</body>
</html>

Kết quả

Trước khi nhấn:

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

Sau khi nhấn "Lấy giá trị Type":

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

ℹ️ Lưu ý: Luôn trả về "month". Dùng trong code xử lý form động để phân biệt loại input (text, date, month, week, datetime-local...).