下拉菜单打开所有子菜单

时间:2016-08-26 22:14:42

标签: jquery html css drop-down-menu

我创建了一个带MDL的侧边栏菜单,目前我正在尝试解决下拉菜单问题。单击菜单项后,它应该只为一个菜单项打开子菜单,但它会打开它们 - 每个菜单项都有子菜单。

HTML

<li>
     <a class="mdl-navigation__link havesub_nav" href="javascript:void(0);">
                    <i  class="material-icons" role="presentation">create</i>
                                <?php echo $text_accounting; ?>
                                <i class="material-icons arrow_invert" role="presentation">chevron_right</i>
                            </a>
                        </strong>
                        <ul class="sub_nav">
                            <li>
                                <a class="mdl-navigation__link" href="<?php echo $account; ?>">
                                    <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                                    <?php echo $text_account; ?>
                                </a>
                            </li>
                            <li >
                                <a class="mdl-navigation__link" href="<?php echo $currency; ?>">
                                    <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                                    <?php echo $text_currency; ?>
                                </a>
                            </li>
                            <li >
                                <a class="mdl-navigation__link" href="<?php echo $inventory; ?>">
                                    <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                                    <?php echo $text_inventory; ?>
                                </a>
                            </li>
                            <li >
                                <a class="mdl-navigation__link" href="<?php echo $journal; ?>">
                                    <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                                    <?php echo $text_journal; ?>
                                </a>
                            </li>
                        </ul>

Javascript(jQuery)

    $(document).ready(function() {
    $('.havesub_nav').click(function(){
        $('.arrow_invert').toggleClass("transform_arrow");
        $('.sub_nav').slideToggle(200);
    });
});

2 个答案:

答案 0 :(得分:1)

这是因为您引用了所有元素,例如:

$('.arrow_invert').toggleClass("transform_arrow");

使用本地搜索更改此类行:

$(this).find('.arrow_invert').toggleClass("transform_arrow");

一个例子:

$(function () {
  $('.havesub_nav').click(function(){
    $(this).find('.arrow_invert').toggleClass("transform_arrow");
    $(this).next('.sub_nav').slideToggle(200);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<li>
    <a class="mdl-navigation__link havesub_nav" href="javascript:void(0);">
        <i  class="material-icons" role="presentation">create</i>
        <?php echo $text_accounting; ?>
        <i class="material-icons arrow_invert" role="presentation">chevron_right</i>
    </a>
    </strong>
    <ul class="sub_nav">
        <li>
            <a class="mdl-navigation__link" href="<?php echo $account; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_account; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $currency; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_currency; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $inventory; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_inventory; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $journal; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_journal; ?>
            </a>
        </li>
    </ul>
</li>
<li>
    <a class="mdl-navigation__link havesub_nav" href="javascript:void(0);">
        <i  class="material-icons" role="presentation">create</i>
        <?php echo $text_accounting; ?>
        <i class="material-icons arrow_invert" role="presentation">chevron_right</i>
    </a>
    </strong>
    <ul class="sub_nav">
        <li>
            <a class="mdl-navigation__link" href="<?php echo $account; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_account; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $currency; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_currency; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $inventory; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_inventory; ?>
            </a>
        </li>
        <li >
            <a class="mdl-navigation__link" href="<?php echo $journal; ?>">
                <i class="material-icons" role="presentation" style="font-size: 16px; margin-left: 30px; width: 0px;">chevron_right</i>
                <?php echo $text_journal; ?>
            </a>
        </li>
    </ul>
</li>

答案 1 :(得分:0)

快速查看,您可以尝试以下代码吗?

$(document).ready(function() {
    $('.havesub_nav').click(function(){
        $(this).find('.arrow_invert').toggleClass("transform_arrow");
        $(this).next('.sub_nav').slideToggle(200);
    });
});