简体   繁体   English

window.onblur 事件在反应 js 中没有被调用 android webview

[英]window.onblur event in react js not getting invoked in android webview

Code given below works perfectly in PC, Laptop, Tab, and Mobile Browsers.下面给出的代码在 PC、笔记本电脑、Tab 和移动浏览器中完美运行。 This code detects whether the user is opening another tab or other document or losing focus of the current browser window. But if we build a web app from this code using android web-view then this does not work at all.此代码检测用户是否正在打开另一个选项卡或其他文档或失去当前浏览器 window 的焦点。但是如果我们使用此代码使用 android web-view 构建一个 web 应用程序,那么这根本不起作用。

import React,{useEffect} from 'react';

function App() {
useEffect(() =>
{
const onBlurCallback = () => onBlur();
window.addEventListener('blur', onBlurCallback);
return () =>
{
window.removeEventListener('blur', onBlurCallback);
};
}, []);

return (
<div className="App">

</div>
);
}

function onBlur()
{
alert('hello');
}

export default App;

Please suggest a solution that will work in the android web-view also.请提出一个也适用于 android 网络视图的解决方案。

You don't need to use addEventListner for the blur like that in React.您不需要像 React 中那样使用addEventListner进行模糊处理。 You can try it like this:您可以这样尝试:

<input type="text" onBlur={()=> //... your function here} />

Recommend Link: Official Document推荐链接:官方文档

You can use the onBlur function by adding it as an attribute of the <input /> .您可以通过将onBlur function 添加为<input />的属性来使用它。

function Example() {
  return (
    <input
      onBlur={(e) => {
        console.log('Triggered because this input lost focus');
      }}
      placeholder="onBlur is triggered when you click this input and then you click outside of it."
    />
  )
}

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

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