Exclamation mark doesn't work in trigger() when using event namespace in jQuery 1.9

前端 未结 2 431
醉梦人生
醉梦人生 2021-01-05 00:24

Here is the code:

$(\"div\").on(\"click\",function(){
       console.log(\"click\");
});
$(\"div\").on(\"click.plugin\", function(){
       console.log(\"cli         


        
相关标签:
2条回答
  • 2021-01-05 01:09

    Use .$ instead of !

    $("button").click(function() {
          $("div").trigger("click.$");    
    });
    

    Demo [Credits: Tim B James]

    0 讨论(0)
  • 2021-01-05 01:13

    This is how jQuery 1.8.3 looks like:

    trigger: function( event, data, elem, onlyHandlers ) {
    
        // Don't do events on text and comment nodes
        if ( elem && (elem.nodeType === 3 || elem.nodeType === 8) ) {
            return;
        }
    
        // Event object or event type
        var cache, exclusive, i, cur, old, ontype, special, handle, eventPath, bubbleType,
            type = event.type || event,
            namespaces = [];
    
        // focus/blur morphs to focusin/out; ensure we're not firing them right now
        if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
            return;
        }
    
        if ( type.indexOf( "!" ) >= 0 ) {
            // Exclusive events trigger only for the exact event (no namespaces)
            type = type.slice(0, -1);
            exclusive = true;
        }
    
        if ( type.indexOf( "." ) >= 0 ) {
            // Namespaced trigger; create a regexp to match event type in handle()
            namespaces = type.split(".");
            type = namespaces.shift();
            namespaces.sort();
        }
    
        // ...
    

    Notice the "Exclusive events trigger only for the exact event" section.

    And this is jQuery 1.9.1:

    trigger: function( event, data, elem, onlyHandlers ) {
        var handle, ontype, cur,
            bubbleType, special, tmp, i,
            eventPath = [ elem || document ],
            type = core_hasOwn.call( event, "type" ) ? event.type : event,
            namespaces = core_hasOwn.call( event, "namespace" ) ? event.namespace.split(".") : [];
    
        cur = tmp = elem = elem || document;
    
        // Don't do events on text and comment nodes
        if ( elem.nodeType === 3 || elem.nodeType === 8 ) {
            return;
        }
    
        // focus/blur morphs to focusin/out; ensure we're not firing them right now
        if ( rfocusMorph.test( type + jQuery.event.triggered ) ) {
            return;
        }
    
        if ( type.indexOf(".") >= 0 ) {
            // Namespaced trigger; create a regexp to match event type in handle()
            namespaces = type.split(".");
            type = namespaces.shift();
            namespaces.sort();
        }
    
        // ...
    

    Here the entire section is missing (it's also not in the omitted bit).

    It seems as if jQuery has dropped support for this feature. The variable exclusive has been removed from the whole source.

    Looking at the source of version 1.9.1 I don't see a way for you to get the desired functionality without resorting to hacks.

    0 讨论(0)
提交回复
热议问题