我对Ben Alman的hashchange event plugin和我的自定义Tabs_Plugin存在问题。每次触发hashchange事件或单击选项卡链接时,页面都会跳转到页面顶部。
通常我只是通过向链接添加return false
或event.preventDefault
来解决这些问题,但在这种情况下,它对我不起作用。
如果我添加return false
,整个插件就会停止工作。看看:http://gearsdigital.com/sandbox/usecase/tabs/
这是相关的插件代码。我已经在上面包含了hashchange插件(这里没有包含)。
// hashchange plugin here
(function ($) {
var tabs = {
init : function (element, options) {
tabs.menu(element, options);
tabs.navigation(element, options);
$(window).hashchange();
},
// Build tab navigation from headlines
menu : function (element, options) {
var menulist = '';
element.find(options.menusource).each(function () {
var el = $(this), plaintext = el.text(), parent = el.parent(), itemID = plaintext.split(" ").join("").toLowerCase();
menulist += '<li><a href="#' + itemID + '">' + plaintext + '</a></li>';
parent.attr('id', itemID);
});
element.prepend('<ul class="' + options.menuclass + '">' + menulist + '</ul>');
element.find(options.tabbody).wrapAll('<div class="'+options.wrapperclass+'"></div>');
},
navigation : function (element, options) {
$(window).hashchange(function () {
var menu = $('.' + options.menuclass);
var hash = location.hash || menu.find('a:first').attr('href');
if (hash) {
menu.find('a').each(function () {
var that = $(this);
that[that.attr('href') === hash ? 'addClass' : 'removeClass'](options.currentclass);
});
tabs.hidetab(element, options);
tabs.showtab(element, hash, options.onShow);
} else {
menu.find('a:first').addClass(options.currentclass);
}
});
},
showtab : function(element, hash, onShow){
element.find(hash).show(0, onShow);
},
hidetab: function(element, options){
element.find(options.tabbody).hide();
}
};
$.fn.extend({
cctabs: function (config, onShow) {
var defaults = {
wrapperclass: 'tab-content',
currentclass: 'current',
menuclass : 'tabmenu',
menusource: 'h2',
tabbody: '.tab',
onShow: null
};
var options = $.extend(defaults, config);
return this.each(function () {
var element = $(this);
tabs.init(element, options);
});
}
});
})(jQuery);