感叹号不';在jQuery 1.9中使用事件名称空间时,不能在触发器()中工作

感叹号不';在jQuery 1.9中使用事件名称空间时,不能在触发器()中工作,jquery,Jquery,代码如下: $("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").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>
测试。
根据命名空间触发事件
当我离开的时候,它工作了。当我单击按钮时,它会在控制台中记录
单击

但是当我按下按钮时,什么也没有发生。在1.9.1中,感叹号似乎不再起作用了


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

$
代替

$("button").click(function() {
      $("div").trigger("click.$");    
});
[字幕:Tim B James]

这就是它的样子:

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已经放弃了对这个特性的支持。变量
exclusive
已从整个源中删除


查看1.9.1版的源代码,我看不到一种不借助黑客就能获得所需功能的方法。

以前从未见过这种方法。它应该做什么?@juhana也没见过。文档说它应该只触发不带名称空间的处理程序。@bažmegakapa您在文档中读到了什么?你能告诉我吗?我只能找到非官方的提及。无文档功能?@bažmegakapa,该页面的有趣之处在于,它链接到了另一个SO问题。哈哈。但是看到了这个演示,它就工作了。@TimBJames,谢谢,我会把它添加到答案中。@bažmegakapa,看起来像是一个没有记录的黑客。在任何地方都找不到它,我也懒得去挖掘源代码:(jQuery没有使用正则表达式来查找事件名称。这只是偶然发生的。追加
'.$'
之所以有效,是因为
if(type.indexOf(“.”>=0)
部分(请参阅我的答案)。由于没有事件名称空间
“$”
,似乎只触发了
单击
。感谢大家的回答,我们的网站上是否有jQuery工程师,也许我们可以告诉他们将此更改放在jQuery 1.9.1升级指南中。jQuery有。打开一张票据,看看会发生什么。谢谢,Tomalak,我已经发布了关于跟踪系统的问题并得到响应,他们将在升级指南中添加它。这里是链接:在这个jQuery上,似乎正在使用regex来匹配事件名称,不是吗?似乎是这样,但没有相关文档。
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();
    }

    // ...