jQuery select或(select仅目标,无后代)

jQuery selectors (select only target, without descendants)

我只想将事件绑定到 jQuery 中的 "pages"。 问题在于代码不仅针对 "pages" 执行,而且在使用 addClass 时针对所有后代执行。我只想在 addClass 用于 "pages" div 时触发事件。我该怎么做?

//
(function( func ) {
    $.fn.addClass = function() { // replace the existing function on $.fn
        func.apply( this, arguments ); // invoke the original function
        this.trigger('eventAddClass'); // trigger the custom event
        return this; // retain jQuery chainability
    }
})($.fn.addClass); // pass the original function as an argument


$(document).on( "eventAddClass", 'div[data-role="page"]', function(event) {
    //code - do some stuff
});

你可以的。

这是事件冒泡。读这个~ https://www.sitepoint.com/event-bubbling-javascript/

$(document).on( "eventAddClass", 'div[data-role="page"]', function(event) {
    if(event.target !== event.currentTarget) return;

    // your custom code
});