英文:
vue 3 key events on table
问题
在Vue中,我遇到了键盘事件未触发的问题。我知道我可以在body上绑定监听器,但我不明白为什么@keypress
不起作用(我点击某一行然后尝试捕捉'ArrowUp'、'ArrowDown'和其他一些按键事件):
<div @keypress="keyEvt($event)">
<table>
<tr v-for='(row,k) in pageRows()' :key="k" ref="k" @click="selectRow(row,k)" @mouseover="redrawCss(row.id)">
<template v-for="(val,k) in row">
<td :class="k">{{ val }}</td>
</template>
</tr>
</table>
</div>
以及应用方法中的方法:
keyEvt: function(ev){ console.log(ev); }
这永远不会触发。
我知道的唯一有效方法是在mounted生命周期中添加到body上的监听器:
mounted:function() { document.addEventListener( "keydown", this.keyEvt); }
虽然有一些<tr @click
事件来选择行,但事件应该会冒泡,所以即使重绘表格行,父div
仍然应该捕获事件吗?
如果我使用在body上的监听器解决方案,我看到event.srcElement
和target
都指示它被"body"元素捕获了。
我尝试了在行单元格上单击鼠标,然后按键盘上的箭头键,但它不会被表格或其父元素的@keypress
捕获。为什么?有人遇到过同样的问题吗?键盘事件只适用于输入框/文本框/按钮等元素吗?是否有任何特定的原因,导致在模板中的元素上触发事件不起作用?
如果在body上使用事件监听器,即使event.srcElement
是'body'也是正常的吗?这是因为我没有使用可以"聚焦"的输入类型元素吗?
英文:
In vue, i have problem with key events not triggered. I know i can bind listener on body, wonder why does the @keypress not work (i click some row and then try to catch 'ArrowUp','ArrowDown' and some other case keypress events):
<div @keypress="keyEvt($event)">
<table>
<tr v-for='(row,k) in pageRows()' :key="k" ref="k" @click="selectRow(row,k)" @mouseover="redrawCss(row.id)" >
<template v-for="(val,k) in row">
<td :class="k">{{ val }}</td>
</template>
</tr>
</table>
</div>
and method in app methods:
keyEvt: function(ev){ console.log(ev); }
this never triggers
Only way i know works, is adding listener to body (on mounted)
mounted:function() { document.addEventListener( "keydown", this.keyEvt); }
There are some <tr @click events to select row, but events should bubble up, so even redrawing table rows, parent div sholud still catch the event?
If i use the mounted/body listener solution, i see both srcElement and target to state its catched on 'body' element.
I tried mouse click on row cell, then press arrow on keyboard, it does not get caught by @keypress on table or its parent. Why not? Anyone had the same issue? Are key event only working for input/textare/button types of elements? Is there any specific reason why event triggering on elements in template may stop wo(r)king?
In case of using the event listener on body, even event.srcElement is 'body'. Is that normal? Is that because i dont use input-type element that can be "focused" ?
答案1
得分: 1
请查看keydown的描述:
键盘事件仅由
<input>
、<textarea>
、<summary>
以及具有contentEditable或tabindex属性的元素生成。如果未被捕获,它们会冒泡到DOM树,直到达到Document。
所以,除非在表格中放置一个输入框(或类似的元素),否则键盘事件不会在其中生成,并且@keypress
/@keydown
/@keyup
监听器永远不会被触发。
英文:
Have a look at the description for keydown:
>Keyboard events are only generated by <input>
, <textarea>
, <summary>
and anything with the contentEditable or tabindex attribute. If not caught, they bubble up the DOM tree until they reach Document.
So unless you put an input (or something similar) in your table, key events will not be generated inside it, and @keypress
/@keydown
/@keyup
listeners will never be triggered.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论