jquery - 无法使用 FireFox 中的事件对象访问 relatedTarget
问题描述
我正在CodeMirror
为 YAML 使用编辑器。我有下面的代码。
$('#' + element.id).ready(function() {
editor = CodeMirror.fromTextArea(document.getElementById(element.id), Options);
editor.setValue(self.test());
editor.on('blur', function(event) {
var clickedElement = event.relatedTarget;
var isAddDialogButtonClicked = false;
if (clickedElement !== null) {
isAddDialogButtonClicked = clickedElement && clickedElement.parentElement &&
clickedElement.parentElement.id === bindingContextData.addDialogButtonId();
}
});
});
在event.relatedTarget
FireFox 中不起作用。它在 Chrome 中运行良好。关于如何解决这个问题的任何想法?
解决方案
您可以使用 jQuerys 事件访问本机 DOM event.originalEvent
,也许您可以在其中找到相关的目标?同样根据MDN,event.relatedTarget
仅适用于某些事件。复制自 MDN 2018-12-07:
The MouseEvent.relatedTarget read-only property is the secondary target for the mouse event, if there is one.
Event name target relatedTarget
focusin The EventTarget receiving focus The EventTarget losing focus
focusout The EventTarget losing focus The EventTarget receiving focus
mouseenter The EventTarget the pointing device entered to The EventTarget the pointing device exited from
mouseleave The EventTarget the pointing device exited from The EventTarget the pointing device entered to
mouseout The EventTarget the pointing device exited from The EventTarget the pointing device entered to
mouseover The EventTarget the pointing device entered to The EventTarget the pointing device exited from
dragenter The EventTarget the pointing device entered to The EventTarget the pointing device exited from
dragexit The EventTarget the pointing device exited from The EventTarget the pointing device entered to
For events with no secondary target, relatedTarget returns null.
它blur
在那里没有,但我很确定blur
它只是focusout
.
或者,直接挂钩到 vanilla javascript,jQuery 被高估了:
editor[0].addEventListener( 'blur', function( event ){
// whatevs
}
(editor[0]
从返回的 jQuery 对象访问本机 DOM 元素)
否则,重构?
推荐阅读
- django - Django 框架:URL 错误?我的导师不知道
- r - 在循环内使用 n-1 计算
- javascript - 在 html 页面中编写所有 js 代码并将该 html 页面包含在另一个 html 页面而不是 .js 文件中是一种好方法吗?
- python - NumPy:将多维索引的子集传递给数组
- postgresql - 尝试创建对我的 postgres 数据库的复制时出错
- android - React Native 版本 0.64 - 找不到 `FBReactNativeSpec` 的 podspec
- php - 警告:stream_socket_enable_crypto():对等证书
- mysql - 使用子属性的连接更新所有
- python - 使用 C++ API for CPLEX 将由元组数组索引的变量添加到建模
- webvtt - WebVTT:Firefox 87.0 字幕中的 Escape NOTE