9

这是代码:

$("div").on("click",function(){
       console.log("click");
});
$("div").on("click.plugin", function(){
       console.log("click.plugin");
});
$("button").click(function() {
      $("div").trigger("click!");    
});

和 HTML:

<div>test.</div>
<button >Trigger event according to namespace</button>

当我在 jQuery 1.8.3 下运行代码时,它可以工作。当我单击按钮时,它会登录click控制台。

但是当我更改为 jQuery 1.9.1时,按下按钮时没有任何反应。似乎感叹号在 1.9.1 中不再起作用。

我在 1.9 升级指南中找不到此更改。有人知道为什么吗?

4

2 回答 2

5

使用.$代替!

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

演示 [来源:Tim B James]

于 2013-05-09T10:20:22.970 回答
4

这是jQuery 1.8.3的样子:

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();
    }

    // ...

请注意“仅针对确切事件触发的独家事件”部分。

这是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();
    }

    // ...

这里缺少整个部分(它也不在省略的位中)。

好像 jQuery 已经放弃了对这个特性的支持。该变量exclusive已从整个源中删除。

查看版本 1.9.1 的源代码,我看不出有一种方法可以让您在不求助于黑客的情况下获得所需的功能。

于 2013-05-09T11:12:20.587 回答