다음은 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>
출력
화살표 비활성화를 클릭하면 스크롤이 화살표와 함께 작동하지 않습니다 -