【发布时间】:2019-11-19 13:44:07
【问题描述】:
我正在使用文本区域制作一个基本的在线编辑器,我希望编辑器能够监听 keydown 事件。我试图让代码监听按下的 tab 键,然后在 textcontent 之间添加空格(4 个空格),但它不起作用。我该怎么办?
<!DOCTYPE html>
<html>
<head>
<title>Editor</title>
<link rel="stylesheet" href="stylesheet.css">
<link rel="shortcut icon" href="">
<script>
document.addEventListener("keydown", logKey);
function logKey(key){
if (key.keyCode == "9"){
myTextArea.textContent += ' ';
}
}
</script>
</head>
<body>
<header>
<div class="navBar" id="heading-container"></div>
</header>
<textarea id="myTextArea">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent semper in nunc at mattis. Suspendisse metus augue, pellentesque finibus luctus dictum, tempor id nunc.
</body>
</html>
【问题讨论】:
-
如果我没有在文本区域的结束按 Tab 怎么办?我强烈建议您不要尝试重新发明这个轮子。使用 CodeMirror 或类似的。
-
尝试按键事件。
-
你还需要取消操作
标签: javascript html css events textarea