如何在新页

时间:2016-03-27 14:13:11

标签: javascript jquery wordpress drop-down-menu

我正在使用Wordpress模板,其中一项功能是标题中的下拉菜单。单击箭头将其打开,它将保持打开状态,直到您单击箭头将其关闭。但是,当您单击其中一个链接时,它仍然在下一页上保持打开状态。您必须单击箭头才能将其关闭。

我希望下拉菜单在导航到新页面时自动关闭。这是我导航的js ...我按照Lynda的教程进行操作,所以我没有编写这段代码,但我还没有足够的经验来了解js以了解代码的哪一部分正在做什么。

如果您需要任何其他信息,请与我们联系。

function initMainNavigation( container ) {
    // Add dropdown toggle that display child menu items.
    container.find( '.menu-item-has-children > a' ).after( '<button class="dropdown-toggle" aria-expanded="false">' + screenReaderText.expand + '</button>' );

// Toggle buttons and submenu items with active children menu items.
container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );

container.find( '.dropdown-toggle' ).click( function( e ) {
    var _this = $( this );
    e.preventDefault();
    _this.toggleClass( 'toggle-on' );
    _this.next( '.children, .sub-menu' ).toggleClass( 'toggled-on' );
    _this.attr( 'aria-expanded', _this.attr( 'aria-expanded' ) === 'false' ? 'true' : 'false' );
    _this.html( _this.html() === screenReaderText.expand ? screenReaderText.collapse : screenReaderText.expand );
} );
}

initMainNavigation( $( '.main-navigation' ) );

// Re-initialize the main navigation when it is updated, persisting any existing submenu expanded states.
$( document ).on( 'customize-preview-menu-refreshed', function( e, params ) {
    if ( 'primary' === params.wpNavMenuArgs.theme_location ) {
        initMainNavigation( params.newContainer );

    // Re-sync expanded states from oldContainer.
    params.oldContainer.find( '.dropdown-toggle.toggle-on' ).each(function() {
        var containerId = $( this ).parent().prop( 'id' );
        $( params.newContainer ).find( '#' + containerId + ' > .dropdown-toggle' ).triggerHandler( 'click' );
    });
}
});

2 个答案:

答案 0 :(得分:0)

请使用以下链接获取wordpress的制作菜单。 http://cssmenumaker.com/blog/wordpress-3-drop-down-menu-tutorial

阅读上面给出的说明并逐步应用。

答案 1 :(得分:0)

我知道这个问题差不多已经有一年了,但我想继续发布一个答案,万一以后有人需要它。

您的代码中的以下行是当您转到新页面时导致切换保持打开的原因。

// Toggle buttons and submenu items with active children menu items.
container.find( '.current-menu-ancestor > button' ).addClass( 'toggle-on' );
container.find( '.current-menu-ancestor > .sub-menu' ).addClass( 'toggled-on' );

当您导航到属于子菜单的页面时,这些行会自动将“toggle-on”类添加到该页面的相应切换按钮和父子菜单。如果您注释掉这些行,则导航到新页面时子菜单将关闭。