2011-03-09 62 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