简体   繁体   English

从另一个jQuery插件调用jQuery插件

[英]Calling a jQuery plugin from another jQuery plugin

I have an HTML page that loads both the CSS and JS files for two plugins. 我有一个HTML页面,可同时加载两个插件的CSSJS文件。 I need to call plugin1 's function from within plugin2 's JS file. 我需要从plugin2的JS文件中调用plugin1的函数。

Below is part of the plugin ( plugin2 ), specifically the event that needs to fire the function from the other plugin (near the end). 以下是插件( plugin2 )的一部分,特别是需要从另一个插件(接近尾声)触发功能的事件。

onOk      : function () {
    var pre, element;
    pre = element = this.element;

    if (this.insertMode) {
        if (shighlighter.getTag() !== 'pre') {
            element = this.element.getChild(0);
        }
    }
    else {
        pre = element.getAscendant('pre', true);
    }

    this.commitContent(element);

    shighlighter.setCls(pre.getAttribute("data-pbcklang") + " " + editor.settings.cls);

    element.setAttribute('class', shighlighter.getCls());

    if (this.insertMode) {
        editor.insertElement(pre);

        /* START: HERE IS THE FUNCTION I NEED TO CALL FROM THE OTHER PLUGIN. */

        $('pre code').each(function(i, block) {
            hljs.highlightBlock(block);
        });

        /* END: HERE IS THE FUNCTION I NEED TO CALL FROM THE OTHER PLUGIN. */
    }
}

The above doesn't work, so I was wondering how I can change that. 上面的方法不起作用,所以我想知道如何更改它。

Thanks. 谢谢。

You need to load the plugins in the correct order. 您需要以正确的顺序加载插件。 The global variable hljs must be defined when used, so it should suffice to put the script tag for plugin1 (hljs) before the script tag for plugin2. 全局变量hljs在使用时必须定义,因此应将plugin1(hljs)的脚本标记放在plugin2的脚本标记之前就足够了。

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

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