問題描述
我正在為我正在處理的項目構建一個自定義 jQuery 插件.我想將一個自定義的對象返回給另一個 jQuery 插件......而不是必須確保使用我的插件的每個頁面也有這個另一個插件,是否可以將它包含在實際的插件本身中?
而不是在使用我的插件的每個頁面上鍵入以下內(nèi)容:
我想看看是否有一個選項可以允許:
(函數(shù)($){$.include('url_to_plugin');//實現(xiàn)我的插件的代碼})(jQuery);
感謝 Marko 的建議...我可能做錯了,因為我引用的插件未被識別.以下是我的代碼:
(函數(shù)($){var script = document.createElement('script');script.type = '文本/javascript';script.src = 'jquery.simplemodal.js';//和我的插件在同一個目錄//都試過了document.body.appendChild(腳本);document.getElementsByTagName('head')[0].appendChild(script);$.fn.SUI_CheckProgress = 功能(選項){返回 this.each(function() {var obj = $(this);var nDiv = $('');nDiv.modal();//錯誤 nDiv.modal() 不是函數(shù)$(obj).append(nDiv);}};});我嘗試在實際函數(shù)調(diào)用的內(nèi)部和外部聲明腳本(理想情況是全局發(fā)生).如果我在我調(diào)用的頁面中引用腳本,我驗證了代碼是否正確執(zhí)行,但是當我刪除引用時它會失敗.任何有關以編程方式將腳本添加到頁面的其他信息將不勝感激.
已修復:我的問題是我引用了與 js 插件相同的目錄,但是當它添加到頁面時,它位于該目錄之外.
曾經(jīng)
script.src = 'jquery.simplemodal.js';
固定
script.src = 'js/jquery.simplemodal.js';
解決方案 當然 - 您可以創(chuàng)建一個新的腳本元素并將其附加到頭部.
var script = document.createElement('script');script.src = 'url-to-plugin';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="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);
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.
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 = 'jquery.simplemodal.js';
fixed
script.src = 'js/jquery.simplemodal.js';
解決方案 Sure - you can create a new script element and append it to the head.
var script = document.createElement('script');
script.src = 'url-to-plugin';
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 插件中包含外部插件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!