HTML DOM 选择自动聚焦属性
HTML DOM 选择自动聚焦属性可返回并修改页面加载时下拉列表是否获得焦点。
语法
以下是语法 -
1. 返回自动聚焦
object.autofocus
修改自动聚焦
object.autofocus = true | false
示例
让我们看一个 HTML DOM 选择自动聚焦属性的示例 -
<!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 Property Example</h1> <p>Hi, Select your favourite subject:</p> <select class='drop-down' name="Drop Down List"> <option>Physics</option> <option>Maths</option> <option>Chemistry</option> <option>English</option> <option>Economics</option> <option>Hindi</option> <option>Biology</option> </select> <br> <button onclick="disable()" class="btn">Disable Autofocus</button> <script> function disable() { document.querySelector(".drop-down").autofocus = false; } </script> </body> </html>
输出
这将产生以下输出 -
点击“禁用自动聚焦”按钮以禁用下拉列表上的自动聚焦。
广告