在jQuery 1.9中使用事件命名空间时,感叹号在trigger()中不起作用
这是代码:
$("div").on("click",function(){ console.log("click"); }); $("div").on("click.plugin", function(){ console.log("click.plugin"); }); $("button").click(function() { $("div").trigger("click!"); });
和HTML:
test.
当我在jQuery 1.8.3下运行代码时 ,它可以工作。 当我单击按钮时,它会在控制台中click
日志。
但是当我改为jQuery 1.9.1时 ,按下按钮时没有任何反应。 似乎感叹号在1.9.1中不再起作用了。
我在1.9升级指南中找不到此更改。 有人知道为什么吗?
使用.$
而不是!
$("button").click(function() { $("div").trigger("click.$"); });
演示 [演职员:Tim B James]
这就是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已经放弃了对此function的支持。 变量exclusive
已从整个源中删除。
查看版本1.9.1的源代码,我没有看到一种方法让您在不诉诸黑客的情况下获得所需的function。