简体   繁体   English

ReactJS - 如何在图标上的 onClick 事件后不关注 select 元素

[英]ReactJS - How to not get focus on a select element after an onClick event on an icon

In my react js code, I have an icon with an onClick event.在我的反应 js 代码中,我有一个带有 onClick 事件的图标。 If the user clicks on it, the focus goes to an html select element just above it on the page.如果用户单击它,焦点将转到页面上它正上方的 html select 元素。 I don't want the focus to go there.我不想把焦点放在 go 那里。 Just on the page itself or where it was before.就在页面本身或之前的位置。 How can I do that?我怎样才能做到这一点?

To focus an element in React, you can use a Ref eg要在 React 中聚焦元素,您可以使用 Ref 例如

const targetRef = useRef(null);

then in your onClick handler just do然后在您的 onClick 处理程序中执行

targetRef.current.focus();

But you might want to figure out why your focus is currently acting the way it is.但是你可能想弄清楚为什么你的注意力目前是这样的。 It seems odd that the focus changes like that with onClick. onClick 的焦点变化似乎很奇怪。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM