使用 JavaScript 禁用文本区域中的箭头键。
以下是使用 JavaScript 在文本区域中禁用箭头键的代码 -
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.result {
font-weight: 500;
font-size: 18px;
color: blueviolet;
}
</style>
</head>
<body>
<h1>Disabling arrow keys in a text area</h1>
<textarea class="AreaText">
Hello world this is some sample text inside the text area element
</textarea>
<div class="result"></div>
<button class="Btn" style="margin: 15px;">Disable arrows</button>
<h3>Click on the above button to disable scrolling using arrows in the above textArea</h3>
<script>
let BtnEle = document.querySelector(".Btn");
let resEle = document.querySelector(".result");
BtnEle.addEventListener("click", () => {
window.addEventListener(
"keydown",
(event) => {
if ([32, 37, 38, 39, 40].indexOf(event.keyCode) > -1) {
event.preventDefault();
}
},
false
);
resEle.innerHTML = "Scrolling using arrow keys is disabled";
});
</script>
</body>
</html>输出

点击禁用箭头时,滚动无法使用箭头 -

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP