[英]Creating jQuery Plugin - Settings Fail
我正在創建我的第一個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);
我想這是第一次讓我感到困惑。
題:
標題導致設置失敗,所以這是另一個問題。
如果設置默認標題和描述,然后創建自定義標題或/和描述,則我的設置似乎可以正常工作。它起作用,它將覆蓋默認值。
但是,正如您所讀到的關於按鈕的簡短if語句...如果在默認或習慣中將我的按鈕切換器設置為false或true,它仍然可以工作並覆蓋默認設置...但是,如果默認和習慣都說:true或false。 ..按鈕的值變為“未定義”。
我不知道該如何說總是讀自定義設置,如果定義了它們卻忘記了默認設置……等等。 我希望你們能理解我,希望我能找到答案。
問候,內納德。
編輯:
好像我需要在海關中完全定義一個按鈕配置,以免出現“未定義” ...如何解決此問題?
默認值示例:
var defaults = {
buttons: {
switcher : false,
nameButton_1 : 'Continue',
nameButton_2 : 'Discard',
}
};
海關示例:
$(function() {
$().miniBox({
title: 'Works flawlessly!',
buttons: {
switcher: true
}
});
});
如果未在自定義設置中定義按鈕名稱,如何仍使用默認按鈕設置?
.first
只是一個jQuery方法。 它從集合中選擇第一個元素。
init
方法是一個已定義的函數,然后立即調用它(在這種情況下,這是沒有意義的),盡管您可以稍后在插件代碼之外調用它(也是沒有意義的)。
至於問題的第二部分,您不是遞歸.extend
擴展對象,因此嵌套對象的值不會得到擴展。 使用true
作為.extend
的第一個參數,以使其更深入: http : //jsfiddle.net/ntdLu/1/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.