aka*_*kao 12 html javascript html5 vue.js
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">
Run Code Online (Sandbox Code Playgroud)
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()
}
},
Run Code Online (Sandbox Code Playgroud)
我是初学日语网站开发人员.抱歉,我不擅长英语.
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">
Run Code Online (Sandbox Code Playgroud)
JS
showInput(id) {
this.displayTitle = "none"
this.displayTitleInput = "inline-block"
this.$nextTick(this.$refs["input_" + id][0].focus())
},
Run Code Online (Sandbox Code Playgroud)
控制台上没有错误,但没有用.
Phi*_*hil 17
您的主要问题是$nextTick采用回调函数但您正在执行
this.$refs["input_" + id][0].focus()
Run Code Online (Sandbox Code Playgroud)
立即.您可以使用正确的代码
this.$nextTick(() => {
this.$refs["input_" + id][0].focus()
})
Run Code Online (Sandbox Code Playgroud)
但是,我认为你会遇到更多问题,你的代码可以变得更简单.
您会发现一个问题是,由于您的样式规则,双击任何节点输入时,所有节点输入都将变为可见.
您可以将"编辑"标志存储node在某个单独的对象上或其中.
下面是一个通过...简化代码的示例
ref一个中使用时v-for循环,并且enter在@keydown事件绑定上使用修饰符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
}
}
})Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
该autofocus属性是你的朋友:
<input type="text" autofocus />
Run Code Online (Sandbox Code Playgroud)