我正在創建我想要用於我的項目的第一個jQuery插件。 但是,我擁有的知識可能不夠。不過,我想繼續創造它......創建jQuery插件 - 設置失敗
所以我有這個部分,我的小插件:
jQuery插件 - 默認設置:
;(function($) {
var defaults = {
title : 'miniBox - Title Spot!',
description : 'miniBox Description Spot. Write a short article or leave it blank!',
buttons: {
switcher : true,
nameButton_1 : 'Continue',
nameButton_2 : 'Discard',
}
};
現在下面我們有此部分:
$.fn.miniBox = function(customs) {
var config = $.extend({}, defaults, customs);
var $first = this.first();
$first.init = function() {
$('body').append(// -->
'<div class="miniBox-wrap">'
+ '<div class="miniBox-frame">'
+ '<div class="miniBox-title"></div>'
+ '<div class="miniBox-content">'
+ '<div class="miniBox-description"></div>'
+ '<div class="miniBox-buttons"></div>'
+ '<div class="miniBox-counter"></div>'
+ '</div>'
+ '</div>'
+ '<div class="miniBox-overlay"></div>'
+ '</div>' // <--
);
var mB_buttonOne = config.buttons.nameButton_1,
mB_buttonTwo = config.buttons.nameButton_2,
mB_title = config.title,
mB_description = config.description;
// --> Confirmation Buttons - Settings OPEN //
if(config.buttons.switcher === true) {
$('.miniBox-buttons').append(// -->
'<input type="button" id="agree" value=' + mB_buttonOne + '>'
+ '<input type="button" id="disagree" value=' + mB_buttonTwo + '>'
// <--
);
} else {
$('.miniBox-buttons').remove();
}
// <-- Confirmation Buttons - Settings CLOSE //
};
$first.init();
};
})(jQuery);
這是混淆了一點點對我來說,是第一次我猜...
問題:
標題導致設置,以便在這裏失敗的另一個問題。
如果我設置了默認標題和描述,然後創建自定義標題或/和描述,那麼我的設置似乎正在工作......它的工作原理將覆蓋默認設置。
但是,如果您可以閱讀簡短的按鈕聲明...如果我的按鈕切換器在默認或習俗中設置爲false或true,它仍然可以工作並覆蓋默認設置......但是,如果默認值和習慣都說: true或false ...按鈕值變成「未定義」。
我沒有線索如何說總是隻讀自定義設置,如果他們被定義,並忘記默認值......或類似的東西。我希望你們瞭解我,希望我能找到答案。
問候,Nenad。
編輯:
看起來像我需要一個按鈕配置完全定義爲海關爲了不得到「未定義」 ......我怎樣才能解決這個問題?默認的
例子:
var defaults = {
buttons: {
switcher : false,
nameButton_1 : 'Continue',
nameButton_2 : 'Discard',
}
};
海關的例子:
$(function() {
$().miniBox({
title: 'Works flawlessly!',
buttons: {
switcher: true
}
});
});
我怎麼能仍然使用按鈕名稱默認按鈕設置,如果它們沒有被定義爲自定義設置?
你是什麼意思,你不明白他們?你寫了,對吧? – 2013-02-17 15:18:13
不...我沒有寫變量和那個.init ... 剛剛看到了另一個插件以及$ .extend({}); – user1658136 2013-02-17 15:20:07