创建快捷键以激活 HTML 元素

front end technologyhtmlweb development更新于 2025/4/4 12:28:17

我们可以设计键盘快捷键来执行某些操作,例如点击链接或按钮,以显示键盘文本。定义元素时,我们可以使用 accesskey 属性为该控制元素指定键盘快捷键。

此属性必须至少包含一个可打印字符,包括可以使用键盘输入的重音符号/其他字符。

但是,不同的浏览器在其平台上使用不同的方式激活 accesskey -

Windows Linux
INTERNET EXPLORER
Alt + key
Alt + Shift + key
N/A
MOZILLA FIREFOX
Alt + Shift + key
Alt + Shift + key
GOOGLE CHROME
Alt + key
Alt + Shift + key
Alt + key
MICROSOFT EDGE
Alt + key
Alt + Shift + key
N/A
OPERA 15+
Alt + key
Alt + key
OPERA 12 Shift + Esc 打开可通过访问键访问的内容列表,然后按下相应的键

示例

在下面的示例中,我们使用访问键为元素添加快捷键。

<!DOCTYPE HTML> <html> <body> <h1>Use the Shortcut keys to access the Content</h1> <p> Press the <kbd>Alt + W</kbd> keys to navigate the following link : </p> <a href="https://www.youtube.com/" accesskey="w" target="">Open Youtube. </a> <p> Press the <kbd>Alt + Z </kbd>keys to focus on the following text field </p> Enter Your Name : <input type="text" name="name" accesskey="z"> <p> Press the <kbd>Alt+S</kbd> keys to click the button to submit the form: </p> <input type="submit" accesskey="s" onclick="alert('Form submitted successfully.')"> </body> </html>

On executing the above script, the access key gets triggered and creates a shortcut key as mentioned in the code.


相关文章


有用资源