Vue 3表格上的关键事件

huangapple go评论67阅读模式
英文:

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.srcElementtarget都指示它被"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):

&lt;div @keypress=&quot;keyEvt($event)&quot;&gt;
 &lt;table&gt;
  &lt;tr v-for=&#39;(row,k) in pageRows()&#39; :key=&quot;k&quot; ref=&quot;k&quot; @click=&quot;selectRow(row,k)&quot; @mouseover=&quot;redrawCss(row.id)&quot; &gt;
	&lt;template v-for=&quot;(val,k) in row&quot;&gt;
      &lt;td :class=&quot;k&quot;&gt;{{ val }}&lt;/td&gt;
    &lt;/template&gt;
  &lt;/tr&gt;
 &lt;/table&gt;
&lt;/div&gt;

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( &quot;keydown&quot;, 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 &lt;input&gt;, &lt;textarea&gt;, &lt;summary&gt; 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.

huangapple
  • 本文由 发表于 2023年6月5日 16:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定