简体   繁体   English

如何从插件外部访问jQuery插件中的函数?

[英]How to access functions in jQuery plugins from outside the plugin?

Take for example a plugin of the form: 以示例的插件为例:

jQuery.autocomplete = function(input, options) {
    function abc(){}
    function def(){}
}

The functions run fine and as expected. 功能正常并且符合预期。 However, I want to be able to invoke function abc(); 但是,我希望能够调用函数abc(); from a $(document).ready(function() {}); 来自$(document).ready(function(){}); statement. 声明。 The following does not work, and I'd really like some advice. 以下不起作用,我真的很喜欢一些建议。

$(document).ready(function() {
    jQuery.autocomplete.abc();
    abc();
    $(this)->abc();
}

When in the init of the plugin there is this... 在插件的init中有这个......

if( !data ) {
    /* Settings Extraction */
    $.extend(s, defaults, options);
    /* Initialize Code */
    /* Data Storage */
    var YoPlugin = $('<div />', {
        text : $this.attr('title')
    });
    $(this).data('YoPlugin', {
        target : $this,
        myInternalFunction: myInternalFunction,
        settings : $.extend({}, s),
        YoPlugin : YoPlugin
    });
    data = $this.data('YoPlugin');
}

You can expose the internal functions like myInternalFunction has demonstrated. 您可以公开myInternalFunction所演示的内部函数。 Getting into the object from an event called on say $('body') leaves 'this' unusably as the body, so... 从一个叫做事件的事件进入对象说$('body')将'this'留给身体,因此......

var multiSel = $('.YoPlugin');
var singleSel = multiSel[0]; //or other way to select the singleton or specific plugin enhanced target
var pluginDataObj = $(singleSel).data('YoPlugin');
var func = pluginDataObj['myInternalFunction'];
func();

I suppose adding a link as an external plugin reference is better ie like init: is declared in the plugin or similar routes via $.fn.YoPlugin.myInternalFunction 我想添加一个链接作为外部插件引用更好,例如init:在插件或类似路由中通过$ .fn.YoPlugin.myInternalFunction声明

Anyway this set of snippets exposes a night of R&D to explore and help understand whatzwhat a lir bir betta. 无论如何,这套片段揭示了一个研发的夜晚,以探索并帮助理解什么是lir bir betta。

Also you definitely need to read all that you can absorb over here... 你肯定需要阅读所有你可以在这里吸收的东西......

http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/ http://alexsexton.com/blog/2010/02/using-inheritance-patterns-to-organize-large-jquery-applications/

Your autocomplete function would have to explicitly make those functions available somehow, possibly by putting references to them on some global object. 您的自动完成功能必须以某种方式显式地使这些功能可用,可能通过在某些全局对象上引用它们。 Otherwise, they're completely hidden from the outside world and they cannot be accessed. 否则,它们完全被外界所隐藏,无法访问它们。

In jQuery private functions (like the ones you described in the question) are generally tied to an instance. 在jQuery中,私有函数(就像你在问题中描述的那些函数)通常与实例绑定。 Inside the function they use a locally scoped variable, or the this keyword to interact with the instance. 在函数内部,它们使用本地范围的变量,或者使用this关键字与实例进行交互。 So, I do not see how calling them from an onload would make sense. 所以,我不知道如何从onload调用它们是有意义的。 If the plugin was developed correctly the author would implement callbacks wherever necessary; 如果插件正确开发,作者将在必要时实现回调; the callbacks would be assigned through the instance, however, not globally. 回调将通过实例分配,但不是全局分配。

If you are writing this plugin, and want this behavior the syntax could look something like this: 如果您正在编写此插件,并希望此行为,语法可能如下所示:

jQuery.autocomplete = function(input, options) {} 
jQuery.autocomplete.abc = function(){};
jQuery.autocomplete.def = function(){};

These functions would be globally accessible trough $.autocomplete . 通过$.autocomplete可以全局访问这些功能。

Ok, when you typed into a box, the jQuery plugin added divs to a tag cloud. 好的,当你输入一个框时,jQuery插件会将div添加到标签云中。 Clicking on one of these "tags" added it to a secondary list. 单击其中一个“标签”将其添加到辅助列表中。 Clicking on the secondary list removed the item from the secondary list. 单击辅助列表会从辅助列表中删除该项目。 I then wanted this click to add the item to the primary list only if it matched the text typed into the box. 然后,我希望此单击将项目添加到主列表,只有它与键入框中的文本匹配。 For this reason, I wanted to run the autocomplete code again. 出于这个原因,我想再次运行自动完成代码。 To get around this, I added a secondary function to my private code with a "click" event rather than a "keydown". 为了解决这个问题,我使用“click”事件而不是“keydown”向我的私有代码添加了辅助功能。 This allowed me to add a trigger("click") to the divs in the secondary list, which triggered the autocomplete action again. 这允许我向辅助列表中的div添加触发器(“单击”),这会再次触发自动完成操作。 Problem solved. 问题解决了。 I couldn't have figured this out without all your help guys. 没有你所有的帮助,我无法理解这一点。 Thank you! 谢谢!

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

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