HLJ 发布于
2021-12-13 15:49:59

js原生代码禁用键盘enter按键实例

通过 js 原生事件的 keyCode 值来判断禁用enter事件实例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <textarea rows="6"></textarea>
</body>
<script>
  let dom = document.querySelector('textarea')
      dom.onkeydown = function(e){
        console.log(e.key)
        console.log(e.code)
        console.log(e.keyCode)
       
        if(e.keyCode == 13){
          e.returnValue = false
          console.log('禁用enter事件')
        }
      }
</script>
</html>
当前文章内容为原创转载请注明出处:http://www.good1230.com/detail/2021-12-13/571.html
最后生成于 2022-10-07 22:27:53