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

HTML DOM Input Image Object: ปุ่มส่งฟอร์มแบบรูปภาพ

HTML DOM Input Image Object แทนอิลิเมนต์ <input type="image"> ใช้สร้างปุ่มส่งฟอร์ม (submit button) ที่เป็นรูปภาพ

รูปแบบการสร้าง (Syntax)

var imageInput = document.createElement("INPUT");
imageInput.setAttribute("type", "image");

คุณสมบัติหลัก (Properties)

คุณสมบัติคำอธิบาย
altคืนค่าและกำหนดข้อความทางเลือก (alt text)
autofocusคืนค่าและกำหนดโฟกัสอัตโนมัติ
defaultValueคืนค่าและกำหนดค่าเริ่มต้น
disabledคืนค่าและกำหนดสถานะปิดใช้งาน
formคืนค่ารีเฟอร์เรนซ์ของฟอร์มที่ครอบคลุม
formActionคืนค่าและกำหนด action URL เฉพาะปุ่มนี้ (override form action)
formEnctypeคืนค่าและกำหนด enctype เฉพาะปุ่มนี้
formMethodคืนค่าและกำหนด method เฉพาะปุ่มนี้ (GET/POST)
formNoValidateคืนค่าและกำหนดข้าม validation เฉพาะปุ่มนี้
formTargetคืนค่าและกำหนด target เฉพาะปุ่มนี้ (_blank, _self, ฯลฯ)
heightคืนค่าและกำหนดความสูงรูปภาพ
nameคืนค่าและกำหนดแอตทริบิวต์ name
srcคืนค่าและกำหนดที่อยู่รูปภาพ (จำเป็น)
typeคืนค่า "image" (อ่านอย่างเดียว)
valueคืนค่าและกำหนดค่า value attribute
widthคืนค่าและกำหนดความกว้างรูปภาพ

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

สร้างปุ่มส่งฟอร์มแบบรูปภาพ:

<!DOCTYPE html>
<html>
<head>
    <style>
        body { text-align: center; background-color: #363946; color: #fff; }
        .btn { background-color: #db133a; border: none; height: 2rem; border-radius: 50px; width: 60%; margin: 2rem auto; display: block; color: #fff; outline: none; cursor: pointer; }
    </style>
</head>
<body>
    <h1>ตัวอย่าง DOM Input Image Object</h1>
    <button onclick="createIframe()" class="btn">สร้างปุ่มส่งแบบรูปภาพ</button>
    <script>
        function createIframe() {
            var imageInput = document.createElement("INPUT");
            imageInput.setAttribute("type", "image");
            imageInput.setAttribute("src", "https://www.tutorialspoint.com/jdbc/images/jdbc-mini-logo.jpg");
            document.body.appendChild(imageInput);
        }
    </script>
</body>
</html>

คุณสมบัติ form* (formAction, formMethod, ฯลฯ)

คุณสมบัติเหล่านี้ช่วยให้ปุ่มรูปภาพแต่ละปุ่มในฟอร์มเดียวกันสามารถมีพฤติกรรมต่างกันได้ (ส่งไป URL ต่างกัน, method ต่างกัน) โดยไม่ต้องใช้ JavaScript