无法访问对象上的 jQuery 个事件

Can't access jQuery events on object

我正在尝试检查附加到事件处理程序中的对象的事件。为什么下面的检查会产生未定义的结果?

HTML:

<button>Print out events in console</button>

JS:

$('button').on('click', function(ev) {
    console.dir($._data($(ev.target), 'events')); // undefined -- Why?
});

JSFiddle

它必须像下面这样,因为第一个参数是 DOM 元素:

$('button').on('click.myNamespace', function(ev) {
    console.dir($._data(ev.target, 'events')); // undefined -- Why?
});

More info from jQuery Blog $(element).data(“events”): In version 1.6, jQuery separated its internal data from the user’s data to prevent name collisions. However, some people were using the internal undocumented “events” data structure so we made it possible to still retrieve that via .data(). This is now removed in 1.8, but you can still get to the events data for debugging purposes via $._data(element, "events"). Note that this is not a supported public interface; the actual data structures may change incompatibly from version to version.

您传递给 _data 方法的元素应该是 html 元素,但您传递的是 jQuery 对象。试试这个。

$('button').on('click.myNamespace', function(ev) {
    console.dir($._data(ev.target, 'events')); // undefined -- Why?
});

试试这个:

https://jsfiddle.net/ke36mr41/2/

$('button').on('click.myNamespace', function(ev) {
    console.dir($._data(ev.target, 'events')); // undefined -- Why?
});

你应该试试这个:

 jQuery._data( elem, "events" );

因为elem 应该是一个 HTML 元素我建议你之前这样做:

var elem = createElement("div");
elem.innerHtml = $('button').html();

这是有效的

jQuery._data( $('button').get(0), "events" )