javascript - 为什么我的焦点/蓝色事件监听器没有触发?
问题描述
我不认为我在这里做错了什么或很时髦,但它不会触发什么……
const elm = (this.agGridElm.nativeElement as HTMLElement);
elm.addEventListener('focus', (focusEvent: FocusEvent) => {
elm.classList.add('focused');
}, { capture: true, passive: true });
elm.addEventListener('blur', (focusEvent: FocusEvent) => {
elm.classList.remove('focused');
}, { capture: true, passive: true });
我尝试使用或不使用第三个参数{ capture: true, passive: true }
,结果是一样的......奇怪的是,当我在 Chrome 调试器中观察时,我可以看到我的听众正在注册Event Listeners
,点击我的元素,我可以看到两者focus
并被blur
迷住了,但是只是不会触发。
我做了类似的事情mouseleave
并且完美地工作,那么focus
和怎么了blur
?下面的块工作......
elm.addEventListener('mouseleave', (mouseEvent: MouseEvent) => {
console.log('Hi there!');
});
解决方案
原来原因是因为focus,
模糊,focusin
在focusout
元素上div
,他们需要有一个tabindex
焦点才能工作!解决方案是将以下内容添加到我希望收听的元素中。
<div tabindex="-1"></div>
我所有的代码都与tabindex
!
推荐阅读
- c# - 如何设置用户控制按钮的背景颜色?以及如何将参数传递给它的事件处理程序?
- regex - 匹配不被大括号括起来的单个字符
- ios - 如何在视图控制器被关闭后保持对通知的响应
- python - 创建用户后,上传带有用户详细信息的 csv 文件并发送重置密码电子邮件 Django
- swift - 点击单元格时隐藏另一个单元格中的按钮
- r - 如何将日期从一个数据帧插入另一个?
- angular - 将数组或对象添加到 Firestore 文档
- python - Python:.strip() 和 .write() 似乎会产生空格
- r - R中的foreach / forvalues循环
- c# - 如何在 UWP 项目中捕获我所在位置的经度和纬度?