如何通过事件对象从 originalEvent 传递数据?
How to pass data from originalEvent through an event object?
我需要通过事件对象传递 originalEvent: MouseEvent { ... }
。通常你可以这样做:
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20
);
很遗憾,以下内容不起作用:
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20,
originalEvent: MouseEvent {
isTrusted: true,
clientX: 115,
clientY: 20,
layerX: 115,
layerY: 20,
pageX: 115,
pageY: 20,
screenX: 115,
screenY: 20,
x: 115,
y: 20
},
pageX: 115,
pageY: 20
});
错误:
SyntaxError: Unexpected token '{'. Expected '}' to end an object literal.
这主要是语法问题。您需要使用 new
关键字调用 constructor of MouseEvent
。您还需要提供第一个参数作为引发的 MouseEvent 的类型,然后第二个参数是包含事件属性的对象;我在下面的示例中使用了 click
,但这很容易更改。
另请注意,isTrusted
是只读的 属性,您不能以编程方式设置它。
$('#name').on('mousedown', function(e) {
console.log(e.originalEvent);
});
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20,
originalEvent: new MouseEvent('click', {
clientX: 115,
clientY: 20,
layerX: 115,
layerY: 20,
pageX: 115,
pageY: 20,
screenX: 115,
screenY: 20,
x: 115,
y: 20
}),
pageX: 115,
pageY: 20
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="name">Name</button>
我需要通过事件对象传递 originalEvent: MouseEvent { ... }
。通常你可以这样做:
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20
);
很遗憾,以下内容不起作用:
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20,
originalEvent: MouseEvent {
isTrusted: true,
clientX: 115,
clientY: 20,
layerX: 115,
layerY: 20,
pageX: 115,
pageY: 20,
screenX: 115,
screenY: 20,
x: 115,
y: 20
},
pageX: 115,
pageY: 20
});
错误:
SyntaxError: Unexpected token '{'. Expected '}' to end an object literal.
这主要是语法问题。您需要使用 new
关键字调用 constructor of MouseEvent
。您还需要提供第一个参数作为引发的 MouseEvent 的类型,然后第二个参数是包含事件属性的对象;我在下面的示例中使用了 click
,但这很容易更改。
另请注意,isTrusted
是只读的 属性,您不能以编程方式设置它。
$('#name').on('mousedown', function(e) {
console.log(e.originalEvent);
});
$('#name').trigger({
type: 'mousedown',
clientX: 115,
clientY: 20,
originalEvent: new MouseEvent('click', {
clientX: 115,
clientY: 20,
layerX: 115,
layerY: 20,
pageX: 115,
pageY: 20,
screenX: 115,
screenY: 20,
x: 115,
y: 20
}),
pageX: 115,
pageY: 20
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="name">Name</button>