【问题标题】:How to extend the jQuery accordion plugin如何扩展 jQuery 手风琴插件
【发布时间】:2012-11-25 23:50:04
【问题描述】:

如何扩展一个 jQuery 插件?

目前我正在使用multiopen accordion 插件。

我需要添加新功能,例如展开/折叠完成后,我需要回调一个函数,例如 jquery ui 手风琴插件中的更改事件。

如何在此插件中添加此功能。

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-plugins jquery-ui-accordion


    【解决方案1】:

    您不需要手风琴小部件。你可以用几行 jQuery 来做到这一点。

    html:

    <h3 class="header"> Title 1 </h3>
    <div class="content"> Content 1 </div>
    <h3 class="header"> Title 2 </h3>
    <div class="content"> Content 2 </div>
    

    javascrpt/jQuery:

    ( function( $ ){ // closure to make sure jQuery = $
      $( function(){ // on document load
        $( ".header" ).click( function( e ){ // select headers and set onClick event handler
          // here you can maybe add a class to an opened header like this
          $( this ).toggleClass( "open" );
          $( this ).next().toggle( "slow", function(){ // toggle visibility
            // what you write here will be executed after the animation
            // "this" will refer to the hidden/revealed div element
            // if you want to call a function depending on if the 
            // panel was opened or closed try this
            if ( $( this ).is( ":visible" ) ) {
              tabOpened( e, this );
            } else {
              tabClosed( e, this );
            }
          }) 
        }).next().hide()
      })
    })(jQuery)
    

    以及在 jsfiddle 上工作的整个过程 http://jsfiddle.net/qpqL9/

    【讨论】:

      【解决方案2】:

      您可以在标签上完成动画的回调函数中轻松调用您的函数。 jquery.multi-accordion-1.5.3.js 的细微变化

      $div.slideDown('fast', function(){
          $div.addClass(options._classes.divActive);
          //function to be called after expanding the tabs. 
      });
      
      $div.slideUp('fast', function(){
          $div.removeClass(options._classes.divActive);
          //function to be called after collapsing the tabs
      });
      

      【讨论】:

      • 好的,我应该在哪里包含以上行以及如何调用我的用户定义的 javascript 函数,如 tabShown: function(event, ui) { }
      • 很遗憾,没有参数发送到function。但是您可以轻松地从那里调用任何函数。
      【解决方案3】:
      $.extend($.ui.multiAccordion, {    
          // private helper method that used to show tabs
          _showTab: function($this) {
              var $span = $this.children('span.ui-icon');
              var $div = $this.next();
              var options = this.options;
              $this.removeClass('ui-state-default ui-corner-all').addClass('ui-state-active ui-corner-top');
              $span.removeClass('ui-icon-triangle-1-e').addClass('ui-icon-triangle-1-s');
              // MODIIFICATION
              bindThis = this;
              var ui = {
                  tab: $this,
                  content: $this.next('div')
              }
              $div.slideDown('fast', function(){
                  $div.addClass(options._classes.divActive);
                  // MODIFICATION
                  bindThis._trigger('tabShownComplete');
              });
              this._trigger('tabShown', null, ui);
          },
      
          // private helper method that used to show tabs 
          _hideTab: function($this) {
              var $span = $this.children('span.ui-icon');
              var $div = $this.next();
              var options = this.options;
              $this.removeClass('ui-state-active ui-corner-top').addClass('ui-state-default ui-corner-all');
              $span.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-e');
              // MODIIFICATION
              bindThis = this;
              var ui = {
                  tab: $this,
                  content: $this.next('div')
              }
              $div.slideUp('fast', function(){
                  $div.removeClass(options._classes.divActive);
                  // MODIFICATION
                  bindThis._trigger('tabHiddenComplete', null, ui);
              });
              this._trigger('tabHidden', null, ui);
          }
      });
      

      【讨论】:

      • 你是对的 ;)。已经很晚了,我没有测试我的代码。我删除了我的答案并赞成你的。干杯。
      • @Nick 感谢您的回答.. 最初 Nupur 的回答帮助了我
      【解决方案4】:

      你尝试过 tabHidden 和 tabShown 方法吗?

       // when tab is shown, ui here hold the same as in click event above
        tabShown: function(event, ui) {}
      
        // when tab is hidden, ui here hold the same as in click event above
        tabHidden: function(event, ui) {}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多