2011-03-09 75 views
0

我与本Alman的hashchange event plugin和我的自定义Tabs_Plugin问题。每当hashchange事件被触发或者我点击了一个标签链接,页面就跳转到页面的顶部。jQuery hashchange插件问题

通常我只是通过将return falseevent.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); 

回答

1

怎么样使用jQuery工具tabs pluginhistory plugin也从jQuery的工具。

+0

这对于我的目的来说有很多开销。我不需要Ajax功能或自定义效果。但我需要能够从给定的标记建立导航。我需要学习:) – gearsdigital 2011-03-09 18:56:49

+1

所以,请描述你想做什么,也许我可以帮助你呢?我使用jQuery和Ajax的hastag插件,所以我认为如果你分享你的propuse我可以帮助:) – 2011-03-10 03:50:35

+0

如果有人正在寻找类似的解决方案,我创建了这个简单的脚本来管理标签:http://codepen.io/ ivanchaer /后/ jQuery的标签 – 2015-11-06 12:53:07