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

คุณสมบัติ autofocus ของ HTML DOM Select

คุณสมบัติ autofocus ของ HTML DOM Select ใช้สำหรับคืนค่าและเปลี่ยนแปลงว่ารายการแบบเลื่อนลงควรได้รับโฟกัสเมื่อโหลดหน้าหรือไม่

รูปแบบไวยากรณ์ (Syntax)

  • 1. การคืนค่า autofocus

object.autofocus
  • การเปลี่ยนแปลง autofocus

object.autofocus = true | false

ตัวอย่าง

ตัวอย่างการใช้งานคุณสมบัติ autofocus ของ HTML DOM Select:

<!DOCTYPE html> <html> <head> <title>HTML DOM autofocus property</title> <style>    body{       text-align:center;       color:#fff;       background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat;       height:100vh;    }    p{       font-weight:700;       font-size:1.1rem;    }    .drop-down{       display:block;       width:35%;       border:none;       font-weight:bold;       padding:10px;       margin:1rem auto;       background-color:#ffffff7a;       outline-color:black;    }    .btn{       background:orange;       border:none;       height:2rem;       border-radius:2px;       width:35%;       margin:2rem auto;       display:block;       color:#fff;       font-weight:bold;       outline:none;       cursor:pointer;    } </style> </head> <body> <h1>ตัวอย่างคุณสมบัติ autofocus</h1> <p>สวัสดีครับ โปรดเลือกวิชาที่คุณชอบ:</p> <select class='drop-down' name="Drop Down List"> <option>ฟิสิกส์</option> <option>คณิตศาสตร์</option> <option>เคมี</option> <option>ภาษาอังกฤษ</option> <option>เศรษฐศาสตร์</option> <option>ภาษาฮินดี</option> <option>ชีววิทยา</option> </select> <br> <button onclick="disable()" class="btn">ปิด Autofocus</button> <script>    function disable() {       document.querySelector(".drop-down").autofocus = false;    } </script> </body> </html>

ผลลัพธ์

โค้ดด้านบนจะให้ผลลัพธ์ดังนี้:

คุณสมบัติ autofocus ของ HTML DOM Select

คลิกปุ่ม 「ปิด Autofocus」 เพื่อปิด autofocus บนรายการแบบเลื่อนลง

คุณสมบัติ autofocus ของ HTML DOM Select