vue.js 将重点放在输入上

vue.js put focus on input

HTML

<span :style="{ display : displayTitle }" @dblclick="showInput()">
  {{ node.title }}
</span>
<input :style="{ display : displayTitleInput }" type="text" 
       @blur="hideInput1" @keydown="hideInput2" 
       @input="changeTitle(node.id , $event.target.value)" 
       :value="node.title">

JS

data() {
  return {
      displayTitle: "inline-block",
      displayTitleInput: "none"
    };
},
showInput() {
    this.displayTitle = "none"
    this.displayTitleInput = "inline-block"
},
hideInput1() {
   this.displayTitle = "inline-block"
   this.displayTitleInput = "none"
},
hideInput2(event) {
    if (event.keyCode === 13) {
        this.hideInput1()
    }
},

我是一名初级日语网络开发人员。我英语不好,抱歉。

HTML 在 "v-for" (v-for="node in list").

双击文字时,变成<input>

我希望能够在输入出现时专注于输入。

我试过了,但没用。

HTML

<span :style="{ display : displayTitle }" @dblclick="showInput(node.id)">
  {{ node.title }}
</span>
<input :ref='"input_" + node.id' :style="{display:displayTitleInput}" type="text" 
       @blur="hideInput1" @keydown="hideInput2" 
       @input="changeTitle(node.id , $event.target.value)" 
       :value="node.title">

JS

showInput(id) {
    this.displayTitle = "none"
    this.displayTitleInput = "inline-block"

    this.$nextTick(this.$refs["input_" + id][0].focus())
},

控制台没有错误,但没有工作。

你的主要问题是 $nextTick 需要一个回调函数,但你正在执行

this.$refs["input_" + id][0].focus()

马上。您可以使用

使您的代码正常工作
this.$nextTick(() => {
  this.$refs["input_" + id][0].focus()
})

但是,我认为您会 运行 遇到更多问题,并且您的代码可以变得更简单。

您会发现一个问题是,由于您的样式规则,当您双击其中任何一个节点时,您的所有节点输入都会变得可见。

您可以将 "editing" 标志存储在 node 或单独对象中的某处。

下面是一个通过以下方式简化代码的示例...

  1. v-for 循环中使用时使用 ref 的类数组性质,并且
  2. 在您的 @keydown 事件绑定上使用 enter 修饰符

new Vue({
  el: '#app',
  data: {
    list: [
      {id: 1, title: 'Node #1'},
      {id: 2, title: 'Node #2'}
    ],
    editing: {}
  },
  methods: {
    showInput(id, index) {
      this.$set(this.editing, id, true)
      
      this.$nextTick(() => {
        this.$refs.input[index].focus()
      })
    },
    hideInput(id) {
      this.editing[id] = false
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<ul id="app">
  <li v-for="(node, index) in list">
    <span v-show="!editing[node.id]" @dblclick="showInput(node.id, index)">
      {{ node.title }}
    </span>
    <input v-show="editing[node.id]" type="text"
           ref="input" :value="node.title"
           @blur="hideInput(node.id)" @keydown.enter="hideInput(node.id)">
  </li>
</ul>

您使用this.$nextTick();的方式不正确。你应该给它传递一个回调函数。

this.$nextTick(function () {
    this.$refs["input_" + id].focus()
})

https://jsfiddle.net/un65e9oc/7/


不过,我不确定该数组访问如何为您工作,因为正如我所注意到的,$refs 是一个对象,其键引用引用名称。

[编辑:感谢@Phil 的评论,上面很清楚。]


以上是您问题的正确解决方案。既然你已经得到了那个答案,我会补充一些其他的东西。

您看到此行为的原因是,当您在 showInput() 方法中更改文本框的可见性时,您保存在 $refs 中的引用不会更新。因此,当您调用 this.$refs["input_" + id].focus(); 时,它实际上是在尝试在隐藏元素上设置 focus(因为当前引用未更新)。

这就是为什么您需要调用 $nextTick() 来更新它。但是如果你想快速解决你的问题,而不调用 $nextTick(),你可以像这样手动更新它:

this.displayTitleInput = "inline-block"
this.$refs["input_" + id].style.display = this.displayTitleInput

this.$refs["input_" + id].focus();

这也行:)希望对您有所帮助!!

autofocus属性是你的朋友:

<input type="text" autofocus />

当我们验证表单并希望动态设置每个字段的焦点时,它对我有用

           this.$validator.validateAll("FORM_NAME").then(valid => {

                var errors = this.$validator.errors;

                if (valid) {
                    console.log('All Fields are valid')
                } else {
                    const errorFieldName = this.$validator.errors.items[0].field;
                    console.log(errorFieldName);
                    this.$refs[errorFieldName].focus();
                }

            });

如果你想在点击某物后设置焦点并使用 vue js 显示带有设置焦点的输入文本框

directives: {
  focus: {
    // directive definition
    inserted: function (el) {
      el.focus()
    }
  }
}

并为其使用自定义指令。如果你需要它应该点击然后设置点击

directives: {
  click: {
    // directive definition
    inserted: function (el) {
      el.focus()
    }
  }
}

并使用它

<input v-focus> or <input v-click>

enter code here