How to include an external plugin inside of another jQuery plugin being authored(如何在正在编写的另一个 jQuery 插件中包含外部插件)
问题描述
我正在为我正在处理的项目构建一个自定义 jQuery 插件.我想将一个自定义的对象返回给另一个 jQuery 插件......而不是必须确保使用我的插件的每个页面也有这个另一个插件,是否可以将它包含在实际的插件本身中?
而不是在使用我的插件的每个页面上键入以下内容:
我想看看是否有一个选项可以允许:
(函数($){$.include('url_to_plugin');//实现我的插件的代码})(jQuery);
感谢 Marko 的建议...我可能做错了,因为我引用的插件未被识别.以下是我的代码:
(函数($){var script = document.createElement('script');script.type = '文本/javascript';script.src = "anF1ZXJ5LnNpbXBsZW1vZGFsLmpz";//和我的插件在同一个目录//都试过了document.body.appendChild(脚本);document.getElementsByTagName('head')[0].appendChild(script);$.fn.SUI_CheckProgress = 功能(选项){返回 this.each(function() {var obj = $(this);var nDiv = $('');nDiv.modal();//错误 nDiv.modal() 不是函数$(obj).append(nDiv);}};});我尝试在实际函数调用的内部和外部声明脚本(理想情况是全局发生).如果我在我调用的页面中引用脚本,我验证了代码是否正确执行,但是当我删除引用时它会失败.任何有关以编程方式将脚本添加到页面的其他信息将不胜感激.
已修复:我的问题是我引用了与 js 插件相同的目录,但是当它添加到页面时,它位于该目录之外.
曾经
script.src = "anF1ZXJ5LnNpbXBsZW1vZGFsLmpz";
固定
script.src = "anMvanF1ZXJ5LnNpbXBsZW1vZGFsLmpz";
解决方案 当然 - 您可以创建一个新的脚本元素并将其附加到头部.
var script = document.createElement('script');script.src = "dXJsLXRvLXBsdWdpbg==";script.type = '文本/javascript';document.getElementsByTagName('head')[0].appendChild(script);
如果这不起作用,可以尝试此页面上的方法,使用 setAttribute 的方法略有不同.
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="dXJsX3RvX215X3BsdWdpbg==" />
<script type="text/javascript" src="dXJsX3RvX3BsdWdpbg==" />
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);
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 = "anF1ZXJ5LnNpbXBsZW1vZGFsLmpz"; //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.
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.
was
script.src = "anF1ZXJ5LnNpbXBsZW1vZGFsLmpz";
fixed
script.src = "anMvanF1ZXJ5LnNpbXBsZW1vZGFsLmpz";
解决方案 Sure - you can create a new script element and append it to the head.
var script = document.createElement('script');
script.src = "dXJsLXRvLXBsdWdpbg==";
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
If this doesn't work, maybe try the method on this page, it's a slightly different approach using setAttribute.
这篇关于如何在正在编写的另一个 jQuery 插件中包含外部插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在正在编写的另一个 jQuery 插件中包含外部插
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01