小编Sam*_*ood的帖子

当父节点中有很多 v-if 时,自定义 v-focus 不起作用

我在组件中定义了自定义指令“focus”:

<script>
    export default {
        name: 'demo',
        data () {
            return {
                show: true
            }
        },
        methods: {
            showInput () {
                this.show = false
            }
        },
        directives: {
            focus: {
                inserted: function (el) {
                    el.focus()
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 html 模板:

<template>
    <div>
        <input type="number" id="readonly" v-if="show">
        <button type="button" @click="showInput" v-if="show">show</button>
        <input type="number" id="timing" v-model="timing" v-if="!show" v-focus>
   </div>
</template>
Run Code Online (Sandbox Code Playgroud)

但是当我点击 时buttoninput#timing不能自动对焦

当我把input#readonlybuttondiv,只使用一个V-如果input#timing可以自动对焦 …

directive autofocus vue.js vuejs2

4
推荐指数
1
解决办法
4717
查看次数

标签 统计

autofocus ×1

directive ×1

vue.js ×1

vuejs2 ×1