jquery - 选择/单击后导航列表不会折叠(移动/选项卡 View )

标签 jquery html css nav

当单击导航栏并选择选项时,它会移动到同一页面中的特定部分,但导航列表(下拉列表)不会折叠或关闭。 可能是调用 none block 的函数没有被执行, 如果有人会指导功能调用并为此进行事件调用,我们将不胜感激。 (移动/标签 View 需要)

以下代码是html、css和js。

因为,我是新手,所以特定部分阻止了它或 wat dnt knw,!

以下代码 html:

<nav class="nav">
                <ul class="nav-list" data-toggle="dropdown">
                    <li class="nav-item"><a href="#home">Home</a></li>
                    <li class="nav-item"><a href="#aboutWrapper">About</a></li>
                    <li class="nav-item"><a href="#services">Products</a></li>
                    <li class="nav-item"><a href="#contact">Contact</a></li>
                </ul>
        </nav>

CSS:

@media only screen and (min-width: 320px) and (max-width: 768px) {
.nav-mobile {
    display:block;
}
.nav {
    width:100%;
    padding:0px 0 0;
}
.nav-list {
    display:none;
}
.nav-item {
    width:100%;
    float:none;
}
.nav-item a {
    background:#34495E;
    padding:10px;
}
.nav-item:first-child a {
    border-radius:5px 0 0;
}
.nav-item:last-child a {
    border-radius:0 0 5px 5px;
}
.nav-active {
    display:block;
}
.nav-mobile-open {
    border-radius:5px 5px 0 0;
    -webkit-border-radius:5px 5px 0 0;
    -moz-border-radius:5px 5px 0 0;
}
}

js代码:

<script>
    (function () {

        // Create mobile element
        var mobile = document.createElement('div');
        mobile.className = 'nav-mobile';
        document.querySelector('.nav').appendChild(mobile);

        // hasClass
        function hasClass(elem, className) {
            return new RegExp(' ' + className + ' ').test(' ' + elem.className + ' ');
        }

        // toggleClass
        function toggleClass(elem, className) {
            var newClass = ' ' + elem.className.replace(/[\t\r\n]/g, ' ') + ' ';
            if (hasClass(elem, className)) {
            alert("if func");
                while (newClass.indexOf(' ' + className + ' ') >= 0) {
                    newClass = newClass.replace(' ' + className + ' ', ' ');
                }
                elem.className = newClass.replace(/^\s+|\s+$/g, '');
            } else {
                elem.className += ' ' + className;
            }

        }

        // Mobile nav function
        var mobileNav = document.querySelector('.nav-mobile');
        var toggle = document.querySelector('.nav-list');
        mobileNav.onclick = function () {
            alert("func ");
            toggleClass(this, 'nav-mobile-open');
            toggleClass(toggle, 'nav-active');              
        };
    })();
</script>

最佳答案

请添加这个belwo mobileNav.onclick

toggle.onclick = function () {
        toggleClass(toggle, 'nav-active');              
    };

这将允许不同的调用。

关于jquery - 选择/单击后导航列表不会折叠(移动/选项卡 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26816729/

相关文章:

javascript - 使用 Angular js在点击事件中调用routeProvider

javascript - 删除在 CSS 文件中设置的 CSS 属性(不是内联 CSS)[jQuery]

jquery - 如何在点击时显示弹出窗口?

javascript - 如何在过滤器中添加/删除类 jQuery?

javascript - 当 jquery 过滤器没有返回结果时在 div 中显示消息

jquery - 在一页纸上禁用滚动

html - 带导航的全屏页面幻灯片

javascript - 从文本区域获取更新值并使用 jQuery 将其插入到 div 中?

javascript - 强制一个 div 到容器内另一个 div 的顶部

Css !important formstyle.css 与 main.css 重叠一个 ul(没有类)