Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

HTML DOM Input Month type Property: ประเภท month

คุณสมบัติ typeของ input type="month" คืนค่าสตริง "month" เสมอ ใช้ยืนยันประเภทของอิลิเมนต์

รูปแบบ (Syntax)

object.type

ตัวอย่างการใช้งาน

แสดงค่า type ของ month input:

<!DOCTYPE html>
<html>
<head>
    <style>
        body { text-align: center; color: #fff; background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat; height: 100vh; }
        p { font-weight: 700; font-size: 1.1rem; }
        input { display: block; width: 35%; border: 2px solid #fff; background-color: transparent; color: #fff; font-weight: bold; padding: 8px; margin: 1rem auto; }
        .btn { background: #0197F6; border: none; height: 2rem; border-radius: 2px; width: 35%; margin: 2rem auto; display: block; color: #fff; outline: none; cursor: pointer; }
        .show { font-size: 1.5rem; color: #db133a; font-weight: bold; }
    </style>
</head>
<body>
    <h1>ตัวอย่าง Input month type property</h1>
    <p>สวัสดี อยากรู้ type ของฉันไหม?</p>
    <input type="month" class="monthInput" required>
    <button type="button" onclick="getType()" class="btn">คลิกเพื่อดูค่า Type</button>
    <div class="show"></div>
    <script>
        function getType() {
            var monthInput = document.querySelector(".monthInput");
            document.querySelector(".show").innerHTML = "type = " + monthInput.type;
        }
    </script>
</body>
</html>

คำแนะนำ

คุณสมบัติ type เป็นแบบอ่านอย่างเดียว (read-only) ไม่สามารถเปลี่ยนแปลงได้หลังสร้างอิลิเมนต์แล้ว

ประเภท input ที่เกี่ยวข้องกับวันที่/เวลา

typeคำอธิบายรูปแบบค่า
dateเลือกวันที่YYYY-MM-DD
monthเลือกเดือน/ปีYYYY-MM
weekเลือกสัปดาห์/ปีYYYY-Www
timeเลือกเวลาHH:MM
datetime-localเลือกวันที่และเวลาYYYY-MM-DDTHH:MM