看到我更新的代碼http://jsfiddle.net/u6WYQ/11/
在$.extend
對其進行處理以增加數量問題之前宣佈的默認選項$.fn.faqToggle.defaults
,請參閱內。每個(具有idx
),而不是外部。每個(參考i
)
調用自定義jquery方法faqToggle
在(function($){ ... }(jQuery))
身體,而不是外界
完整的代碼
(function($){
$.fn.faqToggle = function(options){
// declare the default options before processing them
$.fn.faqToggle.defaults = {
numPrefix: 'Q',
numSubfix: '.',
showTooltip:'Show answer',
hideTooltip:'Hide Answer'
}
var opts = $.extend({},$.fn.faqToggle.defaults, options);
function onClick(){
if ($(this).next('dd').is(":hidden")) { // if the answer is hidden show it
$(this).next('dd').show();
$(this).attr("title","Hide answer"); // update screentip
} else {
$(this).attr("title","Show answer"); // update screentip
$(this).next('dd').hide(); // if the answer is shown hide it
}
}
return this.each(function(i){
var questions = $(this).find('dt');
var answers = $(this).find('dd');
answers.hide(); // hide answers initially
// for incrementing question number, referto inner .each, not the outer
questions.each(function(idx){
$(this).attr("title","Show answer"); // add screen tip
$(this).wrapInner("<span class='faqToggleQues' />");
$(this).prepend("<span class='faqToggleNumber'>"+opts.numPrefix + (idx+1) + opts.numSubfix+"</span>");
$(this).click(onClick);
});
});
};
// apply the custom jquery method in the (function($){ ... }(jQuery))
$('#faqList').faqToggle();
})(jQuery);
對不起KVC,我添加了錯誤的jsfiddle鏈接..它應該是TTP://jsfiddle.net/mindfriction/u6WYQ/ – htmlr
我發現這個問題是一個underclared變量拋出jQuery所以它沒有進行到每個函數的下一個循環.. – htmlr
是的,我已經通過默認變量的數量前綴,你可以切在已發佈的鏈接中修改已修改的代碼 – kvc