简体   繁体   English

触发由不同实例设置的jQuery自定义事件

[英]Trigger a jQuery custom event set by different instance

Is there any way to trigger custom events set by a different jQuery instance? 有什么方法可以触发由其他jQuery实例设置的自定义事件? For example: 例如:

// Load version 1.9.1
...
jQuery191(document).on('customEvent', function () {console.log('Callback fired.')});

// Load version 1.6.2 and setup noConflict
...
jQuery162(document).trigger('customEvent');

While it's not recommended to load more than one version of the library, a simple use-case is a plugin that pulls in a specific version of jQuery if the current instance is lower than expected. 虽然不建议加载库的多个版本,但是一个简单的用例是一个插件,如果当前实例的值低于预期,则该插件可插入特定版本的jQuery。 Any suggestions? 有什么建议么?

You should really take care if you have different instance (even if the same version) on a page, because this could lead to unexpected behavior and could result in memory leaks, eg if you remove an element with jQuery191 that has data ( .data(...) or event listeners) that was stored with jQuery162 this will remain in memory until you reload the page and the other way round. 你真的应该照顾,如果你在页面上有不同的实例(即使相同的版本),因为这可能会导致意外的行为,并可能导致内存泄漏,例如,如果您删除与元素jQuery191有数据( .data(...)或已存储与事件侦听器) jQuery162这将保留在内存中,直到你重新加载页面和其他方式轮。

But back to your question. 但是回到您的问题。 I would not recommend it but you could double the event (trigger it with both versions). 我不推荐这样做,但您可以将事件加倍(在两个版本中都触发它)。 The problem with this approach is that it would slow down everything. 这种方法的问题在于它将减慢一切。 And the other problem is that you have two bubbling phases. 另一个问题是您有两个冒泡阶段。 So if you prevent the event in jQuery191 it will still be executed in jQuery162 and the other way round. 因此,如果您在jQuery191阻止该事件,则该事件仍将在jQuery162执行, jQuery162

So it could be a solution that works for you, but it could also make the things worser. 因此,这可能是一种适合您的解决方案,但也可能使情况变得更糟。 So you need to check your self if this solution is something you can use. 因此,您需要检查自己是否可以使用此解决方案。 Probably you also can remove one of the doubling parts. 可能您也可以删除其中一个加倍部分。

(function() {
    //keep the original functions
    var trigger191 = jQuery191.fn.trigger;
    var trigger162 = jQuery162.fn.trigger;

    jQuery191.fn.trigger = function( evt ) {
        trigger191.apply(this,arguments); //call orginal function

        //only double certain events
        if( evt == "my-custom-event" ) {
            var resultset162 = jQuery162(jQuery191.makeArray( this ));//create a ne result set of the 162 version
            trigger162.apply(resultset162,arguments); //call the original function of 162 to avoid an endless loop
        }
    };

    jQuery162.fn.trigger = function(evt) {
        trigger162.apply(this,arguments); //call orginal function

        //only double certain events
        if( evt == "my-custom-event" ) {
            var resultset191 = jQuery191(trigger162.makeArray( this ));
            trigger191.apply(resultset191,arguments); //call the ofiginal function of 191 to avoid an endless loop
        }
    };

})();

(I hope the code works that way. I pretty sure it should work, but I can't test it right now) (我希望代码能这样工作。我很确定它应该工作,但是现在无法测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM