javascript - react js中的window.onblur事件没有在android webview中被调用
问题描述
下面给出的代码在 PC、笔记本电脑、标签和移动浏览器中完美运行。此代码检测用户是否正在打开另一个选项卡或其他文档或失去当前浏览器窗口的焦点。但是,如果我们使用 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;
请提出一个也适用于 android web-view 的解决方案。
解决方案
推荐链接:官方文档
您可以onBlur
通过将其添加为<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."
/>
)
}
推荐阅读
- python - 如何在matplotlib python上创建带有辅助y轴的分组条形图
- ios - Alamofire:使用相同的密钥发送多个 URL 编码参数
- python-3.x - 如果特定字符串是一行中的最后一个单词,如何跳过一行?
- c++ - qDebug 不输出任何东西
- javascript - javascipt jquery中的简单递归函数不起作用
- php - 更改主机时的 CakePHP 重定向问题
- arrays - 如何将字符串附加到切片内的切片?
- assembly - 不同类型的目标文件
- javascript - JS Promise 拒绝执行 then 块并抛出错误
- javascript - 如何在 Jest 中模拟模块