gpt4 book ai didi

javascript - 在 vue.js 中设置输入元素的焦点

转载 作者:可可西里 更新时间:2023-11-01 01:56:06 25 4
gpt4 key购买 nike

我正在尝试在 Vue.js 中设置输入元素的焦点。我在网上找到了一些帮助,但没有任何解释对我有用。

这是我的代码:

<template>
<form method="post" action="" v-on:submit.prevent="search">
<input type="text" placeholder="Person name" required v-model="name" v-el="nameInput" />
<input type="text" placeholder="Company" required v-model="company" v-el="domainInput" />
<input type="submit" value="Search" class="btn show-m" />
</form>
</template>

<script>
export default {
data () {
return {
contacts: [],
name: null,
company: null
}
},
ready: {
// I tried the following :
this.$$.nameInput.focus();
this.$els.nameInput.focus();
// None of them worked !
}
methods: {
search: function (event) {
// ...

// I also would like to give the focus here, once the form has been submitted.
// And here also, this.$$ and this.$els doesn't work
},
}
}
</script>

我尝试了 this.$$.nameInput.focus();this.$els.nameInput.focus(); 我可以在网上找到目标焦点,但 this.$$ 未定义,this.$els 为空。

如果这有帮助,我正在使用 vue.js v1.0.15

感谢您的帮助。

最佳答案

在 vue 2.x 中你可以用 directive 来解决它.

Vue.directive('focus', {
inserted: function (el) {
el.focus()
}
})

然后你可以在输入和其他元素上使用 v-focus 属性:

<input v-focus>

关于javascript - 在 vue.js 中设置输入元素的焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com