首页 > 解决方案 > 我的子菜单项有时会卡在打开状态?

问题描述

我有一个菜单和移动设备,有时其中一个随机卡在打开并且不会关闭。

有人知道为什么吗?

这是我的Javascript:

        /*** Sets or removes .focus class on an element.*/
    function toggleFocus() {
        if ( event.type === 'focus' || event.type === 'blur' ) {
            let self = this;
            // Move up through the ancestors of the current link until we hit .nav-menu.
            while ( ! self.classList.contains( 'nav-menu' ) ) {
                // On li elements toggle the class .focus.
                if ( 'li' === self.tagName.toLowerCase() ) {
                    self.classList.toggle( 'focus' );
                }
                self = self.parentNode;
            }
        }

        if ( event.type === 'touchstart' ) {
            const menuItem = this.parentNode;
            event.preventDefault();
            for ( const link of menuItem.parentNode.children ) {
                if ( menuItem !== link ) {
                    link.classList.remove( 'focus' );
                }
            }
            menuItem.classList.toggle( 'focus' );
        }
    }

标签: javascriptmobilemenu

解决方案


推荐阅读