简体   繁体   English

如何关闭用jquery打开的所有元素? (行为类似于手风琴菜单)

[英]How to close all elements that are opened with jquery? (Behavior similar to the accordion menu)

I have this html code: 我有这个HTML代码:

<div id="menu">
    <ul id="menuFuncionalidades">
        <li>
            <div class="menuFuncionalidades_categoria">
                <a href="/sttenterprise/">Index</a></div>
        </li>
        <li>
            <div class="menuFuncionalidades_categoria">
                <a href="#">Cadastros</a></div>
            <div class="menuFuncionalidades_content_links">
                <div class="menuFuncionalidades_content_links_descricao">
                    <span class="menuFuncionalidades_content_links_descricao_titulo">Descrição</span>
                    <div class="menuFuncionalidades_content_links_descricao_texto">
                        Lorem Ipsum...
                    </div>
                </div>
                <div class="menuFuncionalidades_content_links_links">
                    <ul><b>Acesso Usuario</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                        <li><a href="#">Alterar</a> </li>
                        <li><a href="#">Consultar</a> </li>
                    </ul>
                    <ul><b>Produto</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                    </ul>
                    <ul><b>Perfil</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                    </ul>
                </div>
            </div>
        </li>
        <li>
            <div class="menuFuncionalidades_categoria">
                <a href="#">Coletores</a></div>
            <div class="menuFuncionalidades_content_links">
                <div class="menuFuncionalidades_content_links_descricao">
                    <span class="menuFuncionalidades_content_links_descricao_titulo">Descrição</span>
                    <div class="menuFuncionalidades_content_links_descricao_texto">
                        Lorem Ipsum...
                    </div>
                </div>
                <div class="menuFuncionalidades_content_links_links">
                    <ul>
                        <b>Coletor 1</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                    </ul>
                    <ul>
                        <b>Coletor 2</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                    </ul>
                    <ul>
                        <b>Coletor 3</b>
                        <li><a href="#">Pesquisar</a> </li>
                        <li><a href="#">Incluir</a> </li>
                    </ul>
                </div>
            </div>
        </li>
    </ul>
</div>

My objective is when i click in a menuFuncionalidades_categoria i close all menuFuncionalidades_content_links that is opened, and opened the right one that is in the same level of the menuFuncionalidades_categoria that i click. 我的目标是当我单击menuFuncionalidades_categoria我关闭所有打开的menuFuncionalidades_content_links ,并打开与我单击的menuFuncionalidades_categoria处于同一级别的menuFuncionalidades_categoria So, i try this, but i have no success: 所以,我试试这个,但我没有成功:

$(document).ready(function () {
    $('#menuFuncionalidades > li > .menuFuncionalidades_categoria').click(function () {
        $("#menu").children().find('.menuFuncionalidades_content_links').each(function () {
            // I need a code here to close only the tags that are opened but is not the current
            if ($(this).css('display') == 'block') {
                $(this).hide();
            }
        });

        $(this).parent().find('.menuFuncionalidades_content_links').slideToggle('normal').css('width', $('#menu').css('width'));
    });
});

What am i missing or doing wrong? 我想念什么或做错什么?

OBS: The behavior that i want, is similar with accordion menu. OBS:我想要的行为与手风琴菜单类似。

DEMO here: http://fiddle.jshell.net/ry9dz/1/ 演示在这里: http//fiddle.jshell.net/ry9dz/1/

As @karim79 says, you can't check "display" that way - it's a css attribute, not an html attribute. 正如@ karim79所说,你不能用那种方式检查“显示” - 它是一个css属性,而不是一个html属性。

On the other hand, if you want to close all other entries, then just do that - no need to check if they are visible first! 另一方面,如果你想关闭所有其他条目,那么就这样做 - 不需要先检查它们是否可见! I also suggest you use a more efficient event handler for all your items, instead of binding it to all of them. 我还建议您为所有项目使用更有效的事件处理程序,而不是将其绑定到所有项目。 Using jQuery 1.7, something like this would work: 使用jQuery 1.7,这样的东西可以工作:

$(document).ready(function () {
  $("#menuFuncionalidades").on("click", ".menuFuncionalidades_categoria", function(event) {
    var selectedMenu = $(event.target).parents("li").find(".menuFuncionalidades_content_links")
      , visible = selectedMenu.is(":visible");

    // Hide all others
    $("#menuFuncionalidades .menuFuncionalidades_content_links").hide();
    // Show this one unless it was already visible
    if(!visible) {
      selectedMenu
        .slideToggle('normal')
        .css('width', $('#menu').css('width'));
    }
    // Prevent the default action
    event.preventDefault();
  });
});

You can hide multiple elements without iterating though all of them to see which ones aren't hidden. 您可以隐藏多个元素,而无需迭代所有元素以查看哪些元素未被隐藏。

Replace; 更换;

$("#menu").children().find('.menuFuncionalidades_content_links').each(function () {
  if ($(this).attr('display') == 'block') {
    $(this).hide();
  }
});

with; 用;

$("#menu").find('.menuFuncionalidades_content_links').hide();

$(this).attr('display') is invalid as there are no "display" attribute and instead of using .css('display') == 'block' you could simply use the psuedo selector :visible . $(this).attr('display')无效,因为没有“ display”属性,您可以仅使用psuedo选择器:visible来代替.css('display') == 'block'

You want the display css property: 您需要display css属性:

if ($(this).css('display') == 'block') {
    $(this).hide();
}

or: 要么:

if ($(this).is(':visible')) {
    $(this).hide();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM