admin管理员组文章数量:1325510
Have this code:
<li v-for="(dog, index) in dogs" :key="index" class=" hover:bg-gray-50" :class="{ 'bg-red-50': index === focus }" @keyup.enter="goToSlug(dog)"> .... </li>
I handle the focus perfectly, but want to run the method goToSlug() on key enter be pressed. It doesn't fire the method.
Have this code:
<li v-for="(dog, index) in dogs" :key="index" class=" hover:bg-gray-50" :class="{ 'bg-red-50': index === focus }" @keyup.enter="goToSlug(dog)"> .... </li>
I handle the focus perfectly, but want to run the method goToSlug() on key enter be pressed. It doesn't fire the method.
Share Improve this question edited Jun 1, 2022 at 17:09 Riza Khan 3,1585 gold badges28 silver badges63 bronze badges asked Aug 12, 2021 at 23:38 Louis BLouis B 3061 gold badge6 silver badges20 bronze badges 1- You need the li element to have focus, for key events to fire. You can use vuejs refs to set focus to that element. – Bergur Commented Aug 13, 2021 at 0:03
1 Answer
Reset to default 6Key presses are only registered on items that have focus.
In order to make an element like a <li>
tag focusable (which natively does not have that ability) you will need to add another attribute called tabindex='1'
(1 being an arbitrary value here, but you can read more up on that here).
So in your case:
<li
v-for="(dog, index) in dogs"
tabindex="1"
:key="index"
class=" hover:bg-gray-50"
:class="{ 'bg-red-50': index === focus }"
@keyup.enter="goToSlug(dog)"
> ....
</li>
Now, in order to register a key press on this (or its siblings) just tab through them and press enter when you have the desired target.
本文标签: javascriptEnter key press on ltligt with Vue3Stack Overflow
版权声明:本文标题:javascript - Enter key press on <li> with Vue3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196441a2431167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论