如何在另一个正在编写的jQuery插件中包含一个外部插件 [英] How to include an external plugin inside of another jQuery plugin being authored

查看:592
本文介绍了如何在另一个正在编写的jQuery插件中包含一个外部插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在开发的项目构建一个自定义jQuery插件。我想返回一个自定义的对象到另一个jQuery插件...而不是必须确保使用我的插件的每个页面也有这个其他插件,是否可以将它包含在实际的插件本身?

I am building a custom jQuery plugin for a project I am working on. I want to return an object that is custom to another jQuery plugin...rather than having to make sure that each page that uses my plugin also has this other plugin, is it possible to include it in the actual plugin itself?

而不是在每个使用我的插件的页面上键入以下内容:

Rather than type the following on each page that uses my plugin:

<script type="text/javascript" src="url_to_my_plugin" />
<script type="text/javascript" src="url_to_plugin" />

我想知道是否有一个选项允许这样的内容:

I wanted to see if there is an option that would allow something like:

(function($) {
          $.include('url_to_plugin');
          //code to implement my plugin
})(jQuery);

感谢Marko的建议......我可能做错了,因为我引用的插件是不被承认。下面是我的代码:

Thanks for the suggestion Marko... I may be doing something wrong because the plugin I am referencing is not being recognized. Below is my code:

(function($) {
     var script = document.createElement('script');
     script.type = 'text/javascript';
     script.src = 'jquery.simplemodal.js'; //in same dir as my plugin
     //tried both
     document.body.appendChild(script);
     document.getElementsByTagName('head')[0].appendChild(script);

     $.fn.SUI_CheckProgress = function(options) {
          return this.each(function() {
               var obj = $(this);
               var nDiv = $('<div>');
               nDiv.modal(); //error nDiv.modal() is not a function
               $(obj).append(nDiv);
          }
     };
});

我尝试在实际函数调用的内部和外部声明脚本(理想情况是全局发生)。如果我在我调用的页面中引用脚本,那么我验证了代码是否正确执行了呃当我删除引用它失败了。有关以编程方式向页面添加脚本的任何其他信息将不胜感激。

I tried declaring the script both inside and outside of the actual function call (the ideal is for it to happen globally). I verified the code executes correctly if I reference the script in the page I am calling from, however when I remove the reference it fails. Any additional info on programatically adding scripts to a page would be appreciated.

已修复:我的问题是我引用的目录与js插件,但是当它被添加到页面时它就在那个目录之外。

Fixed: My issue was that I was referencing the same directory as the js plugin, however when it's added to the page it's outside of that directory.

script.src = 'jquery.simplemodal.js';

已修复

script.src = 'js/jquery.simplemodal.js';


推荐答案

当然 - 你可以创建一个新脚本元素并将其附加到头部。

var script = document.createElement('script');
script.src = 'url-to-plugin';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

如果这不起作用,可以尝试此页面,使用 setAttribute 的方法略有不同。

If this doesn't work, maybe try the method on this page, it's a slightly different approach using setAttribute.

这篇关于如何在另一个正在编写的jQuery插件中包含一个外部插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆