You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vue.js在v-for嵌套v-if场景下实现编辑输入框自动聚焦的问题

问题根因

Vue的响应式DOM更新是异步批量执行的,你在setEditing函数里修改editing变量后,Vue不会立刻渲染出输入框元素,此时直接访问$refs.editInput拿不到已经渲染的DOM节点,所以focus()调用无效。

解决方案1:使用$nextTick等待DOM更新完成

只需要把获取焦点的逻辑包在this.$nextTick回调中,回调会在Vue完成DOM更新后自动执行,就能拿到已经渲染的输入框元素:

setEditing(entry, index) {
  this.editing = index + 1;
  this.entry = entry;
  // 等待DOM更新完成后再操作
  this.$nextTick(() => {
    const el = this.$refs.editInput[index];
    // 加可选链避免极端情况拿不到元素时报错
    el?.focus();
  })
},

解决方案2:自定义v-focus指令(更通用)

如果多处需要自动聚焦功能,可以注册一个自定义指令,自动处理元素显示后的聚焦逻辑,不用每次手动调用方法:

  1. 先在组件内注册局部指令:
export default {
  // 其他组件选项...
  data() {
    return {
      // 你的原有数据
    }
  },
  // 注册自定义指令
  directives: {
    focus: {
      // 元素状态更新时触发
      update(el, binding) {
        // 当指令绑定的值为true时自动聚焦
        if (binding.value) {
          el.focus();
        }
      }
    }
  },
  methods: {
    // 你的原有方法
  }
}
  1. 在输入框上绑定指令即可:
<input
  ref="editInput"
  <!-- 绑定指令,值为当前项是否是编辑状态 -->
  v-focus="editing == index + 1"
  autocomplete="off"
  @change="console.log(entry)"
  class="edit-input"
  @keyup.enter="saveChanges(index)"
  v-model="entry"
/>

额外优化建议

你当前所有列表项的输入框都用了同一个id="edit-input",同一个页面内重复ID不符合HTML规范,建议删除或者改成动态ID:id="'edit-input-' + index"。

内容的提问来源于stack exchange,提问作者yourivdloo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 17:54:02