HTML DOM KeyboardEvent altKey 属性
htmlweb developmentfront end technologydom更新于 2025/12/27 23:40:17
HTML DOM KeyboardEvent altKey 属性返回在 HTML 文档中触发按键事件时是否按下了 ALT 键。
语法
以下是语法 −
event.altKey
让我们看一个 HTML KeyboardEvent altKey 属性的示例 −
示例
<!DOCTYPE html>
<html>
<style>
body {
color: #000;
height: 100vh;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
text-align: center;
}
input {
border: 2px solid #fff;
padding: 8px;
background: transparent;
width: 310px;
border-radius: 20px;
outline: none;
}
::placeholder {
color: #000;
}
.show {
font-size: 1.2rem;
color: #fff;
}
</style>
<body>
<h1>HTML KeyboardEvent altKey Property Demo</h1>
<input type="text" placeholder="Enter your message" onkeydown="display(event)">
<div class="show">
</div>
<script>
function display(event) {
if (event.altKey) {
document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
} else {
document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
}
}
</script>
</body>
</html>
输出

现在开始按文本字段中的任意键,然后观察按下 ALT 键时 altKey 属性如何工作 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

