js 上下左右键移动编辑表格

本文详细描述了如何使用JavaScript的`keydown`事件监听器来控制表格中的输入框,实现键盘方向键控制光标移动和输入框内容的处理,以及计算和设置输入框的数量和布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

window.addEventListener("keydown", this.KeyDown);

监听键盘事件

 KeyDown(event) {
      const e = event || window.event || arguments.callee.caller.arguments[0];
      let key = e.keyCode;		//键盘码值 keycode
      if (key == 13) {
        this.qfFlag = false
        return
      }
      if (key == 37) { //左
        this.textIndex = 0
        this.currentCol--;
        if (this.currentCol < 0) {
          this.currentCol = 0
        }
      } else if (key == 38) {	//上
        this.textIndex = 0
        this.currentLine--;
        if (this.currentLine < 0) {
          this.currentLine = 0
        } else {
          this.currentCol = this.currentCol - 6
        }
      } else if (key == 39) {	//右
        this.textIndex = 0
        this.currentCol++;
        if (this.currentCol > this.htmlLength-1) {
          this.currentCol = this.htmlLength-1
        }
      } else if (key == 40) {	//下
        this.textIndex = 0
        this.currentLine++;
        if (this.currentLine > this.currentLineMax - 1) {
          this.currentLine = this.currentLineMax -1
        } else {
          this.currentCol = this.currentCol + 6
        }
      }
      // console.log(this.currentLine, this.currentCol)
      if (this.htmlList[this.currentCol]) {
        this.htmlList[this.currentCol].focus()
      }
      if (key !== 37 && key !== 39 && key !== 38 && key !== 40) {
        this.textIndex++
        if (this.textIndex === 1) {
          console.log(this.currentCol, this.htmlList[this.currentCol])
          this.htmlList[this.currentCol].value = ''
        }
      }
    },

判断输入框数量

this.$nextTick(() => {
  console.log(document.querySelectorAll('.editInput>input'))
  this.htmlList = document.querySelectorAll('.editInput > input')
  this.htmlLength = document.querySelectorAll('.editInput > input').length
  this.currentLineMax = Math.ceil(this.htmlLength/6)
})

获取表格输入框

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值