Javascript CustomEvent详细信息未通过 [英] Javascript CustomEvent detail not getting passed

查看:67
本文介绍了Javascript CustomEvent详细信息未通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自定义javascript事件。事件正常工作和触发,但我传递的'detail'对象不可用。

I am trying to create a custom javascript event. The event works and fires correctly, but the 'detail' object that I'm passing it is not available.

这是我用来创建和发送的代码事件;

Here's the code i'm using to create and dispatch the event;

var double_tap = new CustomEvent("doubleTap", {
    detail: {
        hello: 'world'  
    },
    bubbles: true,
    cancelable: true
});

this.dispatchEvent(double_tap);

然后我使用jquery向主体添加事件监听器;

Then I'm using jquery to add an event listener to the body;

$('body').on('doubleTap', function( e ) { 
    console.log(e);
});

它会触发,控制台日志会发生,但遗憾的是日志只输出事件详细信息,包括气泡和可取消的属性,但绝不是'detail'对象,因此无法访问信息。

It does fire, and the console log happens, but unfortunately the log only outputs the event details including bubbles and cancelable properties, but never the 'detail' object, so that information is not accessible.

这是一个jsbin作为例子,我在点击事件上创建事件身体让你可以看到控制台; http://jsbin.com/looseroots/6

Here's a jsbin as example, im creating the event on the click event of the body so you can see the console; http://jsbin.com/looseroots/6

我希望能够在触发事件时从'detail'对象获取数据。我究竟做错了什么?我在Chrome中测试了这个,在iOS上测试了Safari

I would like to be able to get the data from the 'detail' object when the event is fired. What am I doing wrong? I have tested this in Chrome, and Safari on iOS

推荐答案

您需要访问originalEvent属性以获取详细信息

You need to access the originalEvent property for the details

console.log(e.originalEvent.detail);

这篇关于Javascript CustomEvent详细信息未通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆