如何从外部链接链接到折叠的手风琴?

时间:2016-12-15 17:27:14

标签: javascript jquery accordion external-links

这款手风琴的效果非常好所以现在,我想从外部链接打开它的内容。然而,尽管我付出了努力,但我无法做出回应。

我知道问题出在JS中,这意味着我无法帮助自己......

有什么想法吗?

<div>
<ul id="cbp-ntaccordion" class="cbp-ntaccordion">
    <li>
        <h2 class="cbp-nttrigger"><a href="#accordion1">accordion1</a></h2>
        <div class="cbp-ntcontent">
            <p>Some Text</p>

        </div>
    </li>
    <li>
        <h2 class="cbp-nttrigger"><a href="#accordion2">accordion2</a></h2>
        <div class="cbp-ntcontent">
            <p>Some Text</p>
        </div>
    </li>
    <li>
        <h2 class="cbp-nttrigger"><a href="#accordion3">accordion3</a></h2>
        <div class="cbp-ntcontent">
            <p>Some Text</p>
        </div>
    </li>
</ul>
</div>



;( function( $, window, undefined ) {

'use strict';

// global
var $body = $( 'html, body' );

$.CBPNTAccordion = function( options, element ) {
    this.$el = $( element );
    this._init( options );
};

// the options
$.CBPNTAccordion.defaults = {};

$.CBPNTAccordion.prototype = {
    _init : function( options ) {
        // options
        this.options = $.extend( true, {}, $.CBPNTAccordion.defaults, options );
        // cache some elements and initialize some variables
        this._config();
        // initialize/bind the events
        this._initEvents();
    },
    _config : function() {

        // the clickable items
        this.$items = this.$el.find( '.cbp-nttrigger' );

    },
    _initEvents : function() {

        this.$items.on( 'click.cbpNTAccordion', function() {
            var $listItem = $( this ).parent();
            if( $listItem.hasClass( 'cbp-ntopen' ) ) {
                $listItem.removeClass( 'cbp-ntopen' );
            }
            else {
                $listItem.addClass( 'cbp-ntopen' );
                $body.scrollTop( $listItem.offset().top );
            }
        } );

    },
    destroy : function() {
        this.$items.off( '.cbpNTAccordion' ).parent().removeClass( 'cbp-ntopen' );
    }
};

var logError = function( message ) {
    if ( window.console ) {
        window.console.error( message );
    }
};

$.fn.cbpNTAccordion = function( options ) {
    if ( typeof options === 'string' ) {
        var args = Array.prototype.slice.call( arguments, 1 );
        this.each(function() {
            var instance = $.data( this, 'cbpNTAccordion' );
            if ( !instance ) {
                logError( "cannot call methods on cbpNTAccordion prior to initialization; " +
                "attempted to call method '" + options + "'" );
                return;
            }
            if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
                logError( "no such method '" + options + "' for cbpNTAccordion instance" );
                return;
            }
            instance[ options ].apply( instance, args );
        });
    } 
    else {
        this.each(function() {  
            var instance = $.data( this, 'cbpNTAccordion' );
            if ( instance ) {
                instance._init();
            }
            else {
                instance = $.data( this, 'cbpNTAccordion', new $.CBPNTAccordion( options, this ) );
            }
        });
    }
    return this;
}; } )( jQuery, window );

0 个答案:

没有答案