【问题标题】:Disable collapse on clicking links单击链接时禁用折叠
【发布时间】:2023-03-05 08:45:01
【问题描述】:

我有 2 个问题。第一个是当用户点击一个链接(Say About Us)时,整个菜单都会关闭。这是一个糟糕的用户体验。我希望它保持打开状态,因为它已经打开了。

第二个问题是用户无法关闭活动的主菜单一旦打开(例如条款、私人或子主菜单)。我们如何解决这两个问题?

JSFiddle 演示:https://jsfiddle.net/edvh0nsk/

HTML:

<div class="container">
    <div class="row">
        <div class="col">
            <ul class="nav police mod-list">
              <li class="item-103 deeper parent"><span class="nav-header "><img class="float-right" src="/images/arrow-down.png"  /><span class="image-title">Terms</span></span>
                <ul class="nav-child unstyled small" style="display: none;">
                    <li class="item-106"><a href="/">About Us</a></li>
                </ul>
              </li>
              <li class="item-107 active deeper parent"><span class="nav-header "><img class="float-right" src="/images/arrow-down.png" /><span class="image-title">Private</span></span>
                  <ul class="nav-child unstyled small" style="display: none;">
                    <li class="item-104"><a href="/">Privacy</a></li>
                    <li class="item-105 current active"><a href="/">FAQ</a></li>
                    <li class="item-108 deeper parent"><span class="nav-header ">Sub Main</span>
                      <ul class="nav-child unstyled small" style="display: none;">
                          <li class="item-109"><a href="/">Child</a></li>
                      </ul>
                    </li>
                  </ul>
              </li>
            </ul>
        </div>
    </div>
</div>

CSS:

.police .active .nav-child {
    display: block !important;
}
.police .active.parent .nav-header{
    font-weight: 600;
}
.police .parent {
    display: block;
    width: 100%;
    cursor: pointer;
    padding: 7px 0;
}
.police .nav-child {
    padding-left: 15px;
    font-size: 14px;
}
.police .nav-child li {
    list-style: none;
    padding: 7px 0;
}
.police .parent span img {
    width: 24px;
}
.police .parent.arrow span img {
    -ms-transform: rotate(180deg); /* IE 9 */
    -webkit-transform: rotate(180deg); /* Safari 3-8 */
    -moz-transform: rotate(180deg);
    -o-transform: rotate(180deg);
    transform: rotate(180deg);
}

jQuery

jQuery('.police .nav-child').hide();
jQuery('.police .parent').click(function(e) {
e.stopPropagation();
    jQuery(this).children('ul').slideToggle();
    jQuery(this).toggleClass("arrow");
});

【问题讨论】:

    标签: javascript jquery css


    【解决方案1】:

    https://jsfiddle.net/70Lkw3an/

    你需要在这里去掉!important

    .police .active .nav-child {
        display: block !important;
    }
    

    这样.slideToggle 就可以真正关闭它了。然后你需要防止点击链接传播到父级,这样它就不会关闭菜单:

    jQuery('a').click(function(e) {
        e.stopPropagation();
    })
    

    为了防止最初隐藏活动的:

    jQuery('.police .nav-child:not(.active > .nav-child)').hide();
    

    【讨论】:

    • 如果我删除了display: block !important;,那么活动菜单就会关闭。我想让活动菜单保持打开状态。知道如何实现这一目标吗?非常感谢。
    • 谢谢。但是,当我将 active' class and current active` 类移动到 Terms 时,它不起作用。
    猜你喜欢
    • 2013-08-09
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2021-04-19
    相关资源
    最近更新 更多