看到我更新的代码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