v-focus

作者: 清霆 | 来源:发表于2021-09-09 16:03 被阅读0次
const vueFocus = {};
vueFocus.install = (Vue) => {
    Vue.directive('focus', {
        inserted(el, binding) {
            if (binding.value) {
                const tagName =
                    typeof binding.value === 'string'
                        ? binding.value.trim()
                        : 'input';
                if (el.tagName.toLowerCase() !== tagName.toLowerCase()) {
                    el = el.querySelector(tagName);
                }
                if (el) {
                    el.focus();
                }
            }
        }
    });
};

export default vueFocus;

相关文章

网友评论

      本文标题:v-focus

      本文链接:https://www.haomeiwen.com/subject/vlaiwltx.html