vue回车切换到下一个控件

mounted(){
      //开启enter监听
      this.addEnterListener();
    },
``
`

```javascript
methods:{
//添加键盘监听事件
      addEnterListener() {
          if (window.__completeEnterBind__) return;
          window.addEventListener("keydown", this.enterCallback);
          window.__completeEnterBind__ = true;
      },
      //移除键盘监听事件
      removeEnterListener() {
          window.removeEventListener("keydown", this.enterCallback);
          window.__completeEnterBind__ = false;
      },
      //处理键盘监听事件
      enterCallback(e) {
          function findFormItem(el) {
            debugger
            //获取enter事件父节点
            const parent = el.parentElement;
            if (!parent) return document.body;
            //找到el-form-item节点
            if (
              parent.className.includes("el-form-item") &&
              parent.className.includes("el-form-item--small")
            ) {
              return parent;
            }
              return findFormItem(parent);
            }
          function findInput(container) {
            debugger
            //获取enter事件的下一个节点
            let nextEl = container.nextElementSibling;
            //如果最后一个 将光标放到确定按钮
            if (!nextEl){
              var dialogSubmit = document.getElementById("dialogSubmit");
              dialogSubmit.focus();
              return; 
            }
            let input = nextEl.querySelector("input");
            //如果下一个节点不能被选中,就下一个节点
            while(input.disabled){
              nextEl = nextEl.nextElementSibling;
              input = nextEl.querySelector("input");
            }
            while (input.id === "el-select") {
              nextEl = nextEl.nextElementSibling;
              if (!nextEl) return;
              input = nextEl.querySelector("input");
            }
            //返回输入框或者单选按钮
            if (input.className.includes("el-input__inner")) return input;
            if (input.className.includes("el-radio__original")) return input;
            }
            //如果是enter就调用上面的方法
            if (e.keyCode === 13) {
              const container = findFormItem(document.activeElement);
              findInput(container) && findInput(container).focus();
            }
          },
}
destroyed(){
           //移除监听事件     
          this.removeEnterListener();
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值