jQuery toggle submenu

元气小坏坏 提交于 2021-01-29 17:57:29

问题


I need some help. I try to create a toggle submenu, using jQuery. However, submenu appears only for a few seconds and then disappears, when I click the parent li.

Here is the code:

<div class="menu">
    <ul>
    <li class="parent">
        <a href="">Hello</a>

        <ul class="submenu">
            <li>
                <a href="">Hello</a>
            </li>
            <li>
                <a href="">Hello</a>
            </li>
        </ul>
    </li>
    <li class="parent">
        <a href="">Hello</a>

        <ul class="submenu">
            <li>
                <a href="">Hello</a>
            </li>
            <li>
                <a href="">Hello</a>
            </li>
        </ul>
    </li>

    </ul>
</div>

And here is the jQuery code

$(function() {
  $(".submenu").hide();

  $(".parent").click(function(){
  $(".submenu").toggle()
});
});

回答1:


Use this for current selected element and find the particular class inside the dom

$(function () {
    $(".submenu").hide();
        $(".parent").click(function (e) {
        e.preventDefault(); // If you need to stop default action 
        $(".submenu", this).toggle(); // OR $(this).find(".submenu").toggle(); 
      });
});

DEMO

try as you asking in comment

$(function () {
    $(".submenu").hide();
    $(".parent").click(function (e) {
        e.preventDefault();
        if (!$(e.target).closest("ul").is(".submenu")) {
            $(".submenu", this).toggle();
        }
    });
});

DEMO

$(function () {
    $(".submenu").hide();
    $(".parent").click(function (e) {
        e.preventDefault();
        if (!$(e.target).closest("ul").is(".submenu")) {
            $(".submenu", this).toggle();
            $(this).siblings(".parent").find(".submenu").hide();
        }
    });
});

NEW Demo




回答2:


Try to pass this as the context to the selector,

$(function () {
    $(".submenu").hide();
    $(".parent").click(function (e) {
        $(".submenu:first", this).toggle();
        return false;
    });
});

At this situation we need both .preventDefault() as well as .stopPropagation() needs to be invoked. So I recommend you to use return false to perform both of that operations.

DEMO


$(function () {
    $(".parent a").click(function (e) {
        var elems = $(this).closest('li');
        if (elems.find('.submenu').length) {
            $(".submenu:first", elems).toggle();
        }
        return false;
    });
});

DEMO


For your new requirement use the below code,

$(function () {
    $(".submenu").hide();
    $(".parent a").click(function (e) {
        var elems = $(this).closest('li');
        elems.siblings('li').find('ul').hide();
        if (elems.find('.submenu').length) {
            $(".submenu:first", elems).toggle();
        }
        return false;
    });
});

DEMO NEW



来源:https://stackoverflow.com/questions/26235928/jquery-toggle-submenu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!