use*_*125 10 javascript focus vue.js
我有2个输入,当用户按Enter键时,希望切换焦点从第一个到第二个.我尝试将jQuery与Vue混合因为我找不到任何功能可以专注于Vue文档中的某些内容:
<input v-on:keyup.enter="$(':focus').next('input').focus()"
...>
<input ...>
Run Code Online (Sandbox Code Playgroud)
但在输入时我看到控制台中的错误:
build.js:11079 [Vue warn]: Property or method "$" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in anonymous component - use the "name" option for better debugging messages.)warn @ build.js:11079has @ build.js:9011keyup @ build.js:15333(anonymous function) @ build.js:10111
build.js:15333 Uncaught TypeError: $ is not a function
Run Code Online (Sandbox Code Playgroud)
yur*_*zui 14
你可以尝试这样的事情:
<input v-on:keyup.enter="$event.target.nextElementSibling.focus()" type="text">
Run Code Online (Sandbox Code Playgroud)
小智 6
在我看来,从@zurzui跟进是使用$refs
API(https://vuejs.org/v2/api/#vm-refs)的一种更干净的选择。
使用$refs
API,可以使您以更简单的方式定位元素,而无需遍历DOM。
归档时间: |
|
查看次数: |
14770 次 |
最近记录: |