自学内容网 自学内容网

jquery折叠菜单效果

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .menu {
            width: 200px;
            background-color: #f0f0f0;
        }

        .item {
            padding: 10px;
            border-bottom: 1px solid #ccc;
            cursor: pointer;
        }

        .title {
            margin: 0;
        }

        .sub-menu {
            display: none;
            padding-left: 20px;
        }

        .sub-menu li {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <div>
        <div class="item active">
            <h2 class="title">菜单项 1</h2>
            <ul class="sub-menu" style="display: block;">
                <li>子菜单项 1</li>
                <li>子菜单项 2</li>
                <li>子菜单项 3</li>
            </ul>
        </div>
        <div class="item">
            <h2 class="title">菜单项 2</h2>
            <ul class="sub-menu">
                <li>子菜单项 1</li>
                <li>子菜单项 2</li>
                <li>子菜单项 3</li>
            </ul>
        </div>
        <!-- 更多菜单项... -->
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.js"></script>
    <script>
        $(document).ready(function () {
            $('.item').click(function () {
                $(this).children('.sub-menu').slideToggle();
                $(this).toggleClass('active');
            });
        });
    </script>
</body>

</html>

请添加图片描述


原文地址:https://blog.csdn.net/qq_36437991/article/details/144362621

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!