简体   繁体   English

在 Vue 3 中单击按钮而不闪烁后,如何将注意力集中在输入上?

[英]How do I keep the focus on an input, after i click on a button without flickering in Vue 3?

Script脚本

const amountRef = ref(null);
const amount = ref(0.1);

const insertSuggestion = (e, value) => {
  e.preventDefault();
  e.stopPropagation();
  amount.value = value;
  amountRef.value.focus();
};

Template模板

<Suggestion
   class="..."
   v-for="suggestion in suggestions"
   :key="suggestion"
   @click="insertSuggestion($event, suggestion)"
>
    {{ suggestion }}
</Suggestion>
<input
   class="..."
   @keyup.enter="handleBuy"
   placeholder="Amount"
   ref="amountRef"
   v-model="amount"
/>

在此处输入图像描述

You could try to capture the blur event and add the classes back if the relatedTarget is one of those buttons.如果relatedTarget是这些按钮之一,您可以尝试捕获模糊事件并重新添加类。 Super psuedo code below:超级伪代码如下:

<input
   class="..."
   @keyup.enter="handleBuy"
   @blur="maybeMimicFocus"
   placeholder="Amount"
   ref="amountRef"
   v-model="amount"
/>
maybeMimicFocus (event) {
  if (event.relatedTarget && event.relatedTarget.tagname === 'btn') {
    this.$refs.amountRef.$el.classList.add('the-classes-that-make-it-look-focused')
  }
}

the browser won't have to repaint the dom and therefore this transaction should be so quick that the naked eye won't see it a difference.浏览器不必重新绘制 dom,因此该事务应该非常快,以至于肉眼不会看到它有什么不同。 the only caveat I can see is if there are animations attached to transitioning properties in which those will have to fire.我能看到的唯一警告是,是否有动画附加到必须触发的过渡属性。

You can re-focus the input element on the blur event:您可以将输入元素重新聚焦在blur事件上:

<input ref="amountRef" @blur="$refs.amountRef.focus()"/>

Edit编辑

You also need to check the relatedTarget of the blur event to make sure you only re-focus when one of the buttons is clicked.您还需要检查blur事件的relatedTarget以确保仅在单击其中一个按钮时重新聚焦。

Note: this solution does not work on Firefox.注意:此解决方案不适用于 Firefox。

<Suggestion
   class="suggestion-btn"
   ...
>
</Suggestion>
<input
   ref="amountRef"
   @blur="onBlur"
/>

onBlur(evt) {
    if (
        evt.relatedTarget &&
        evt.relatedTarget.classList.contains("suggestion-btn")
    ) {
        this.$refs.amountRef.focus();
    }
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮时,如何更改跨度的行以输入并使用angularjs集中每个输入 - On Button Click, how do I change row with span to input and focus each input using angularjs 当我单独单击带有 JavaScript 的按钮时,如何聚焦输入 - How do I focus an input when I click a button with JavaScript alone Vue.js:我如何专注于刚刚未隐藏的输入 - Vue.js: How do I focus to an input just unhidden 即使单击外部的按钮,如何保持输入的焦点 - How do I keep an input focused even when I click a button outside it 我为我的网页设置了焦点状态。 但即使在我完成点击按钮/链接之后,焦点仍然存在,除非我点击空格。 我该怎么做? - I styled focus state for my webpage. But even after I'm done clicking button/link, the focus stays, unless I click whitespaces. How do I make it go? 单击按钮时,将注意力集中在输入框上 - Keep focus on input box when click on button 单击按钮时如何将焦点保持在所选文本上? - How can I keep the focus to my selected text when I click a button? 为什么单击输入(Vue)时焦点不起作用? - Why the focus doesn't work when i click on input (Vue)? 单击按钮后保持对文本区域的关注 - Keep focus on textarea after button click 在同一页面中关闭模式后如何保持用户输入而不重新加载? - How do I keep users input after closing modal in the same page without reload?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM