【问题标题】:Auto-collapse nestled list using jQuery & Bootstrap使用 jQuery 和 Bootstrap 自动折叠嵌套列表
【发布时间】:2015-06-16 01:00:20
【问题描述】:

我的问题是创建一个方法(在单击元素时)折叠嵌套列表中不是目标元素或关联的父容器的所有其他元素。我环顾四周,但对我的清单没有任何作用。

基本上我试图折叠除目标元素之外的所有活动列表元素。

  • 使用引导程序处理很多事情。
  • 我有一种方法可以在点击时激活列表元素。
  • 警报似乎输出了需要影响的元素的 ID,并且没有任何崩溃。有什么建议吗?

列表结构:

 - Building
  - Floor
    - Room

代码:

//JS - Attempt to collapse all elements not clicked:

$('.expandable-menu a').click(function(e) {
    var parent = $("#" + e.target.id).attr('data-parent');
    $(".expandable-menu a").each(function() {
        //if 1. has class active 2.Not target event,  3.Not a parent of target 
        if ($(this).hasClass('active') && $(this).attr('id') !== (e.target.id) && $(this).attr('id') !== (parent)) {
            // alert($(this).attr('id') );
            $(this).removeClass('active').addClass('collapsed');
        }
    });
});
<!--HTML - Example chunk from list:-->

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<ul>
    <li id="level1">
        <a id="title-b0" href="#" data-toggle="collapse" data-parent="#title-loc" data-target="#content-b0" class="collapsed" aria-expanded="false">
            <span class="glyphicon glyphicon-triangle-right"></span>
            Biology
        </a>
        
        <ul id="content-b0" class="nav collapse" style="height: 0px;" aria-expanded="false">
            <li id="level2">
                <a id="title-b0f0" class="" href="#" data-toggle="collapse" data-parent="title-b0" data-target="#content-f0" aria-expanded="true">
                    <span class="glyphicon glyphicon-triangle-bottom"></span> 
                    Floor 4 
                </a>
                
                <ul id="content-f0" class="nav collapse in" style="" aria-expanded="true"></ul>
                <a id="title-b0f1" class="" href="#" data-toggle="collapse" data-parent="title-b0" data-target="#content-f1" aria-expanded="true">
                    <span class="glyphicon glyphicon-triangle-bottom"></span>
                    Floor 3
                </a>
                
                <ul id="content-f1" class="nav collapse in" style="" aria-expanded="true">
                    <li>
                        <a id="room-select" href="#">    
                            <span class="glyphicon glyphicon-unchecked"></span>
                            Room 112
                        </a>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

【问题讨论】:

  • 有很多解决方案,请查看this one
  • 如果您要费力编写代码 sn-p,至少让它有点工作。单击帖子中的“运行”按钮并查看,有多个错误(对于初学者,没有元素具有类 expandable-menu,其次是导致 hjavascript 最终出现在标记中的无效 HTML)
  • @Jamiec soz 代码片段由版主编辑添加...html 块仅作为 html 示例存在
  • @Orbitall - 是的,我现在可以看到了,抱歉。在这种情况下,他们通过将您发布的代码更改为错误的代码 sn-p 对您造成了伤害。
  • 我编辑了代码,试图让它再次工作。我希望现在更好

标签: javascript jquery twitter-bootstrap


【解决方案1】:

这个例子可以帮助你

$(document).ready(function() {
    $('.tree li').each(function() {
        if ($(this).children('ul').length > 0) {
            $(this).addClass('parent');
        }
    });

    $('.tree li.parent > a').click(function() {
        $(this).parent().toggleClass('active');
        $(this).parent().children('ul').slideToggle('fast');
    });

    $('#all').click(function() {

        $('.tree li').each(function() {
            $(this).toggleClass('active');
            $(this).children('ul').slideToggle('fast');
        });
    });

    $('.tree li').each(function() {
        $(this).toggleClass('active');
        $(this).children('ul').slideToggle('fast');
    });

});
a{
	cursor:pointer;
}
.tree ul {
    list-style: none outside none;
}
.tree li a {
    line-height: 25px;
}
.tree > ul > li > a {
    color: #3B4C56;
    display: block;
    font-weight: normal;
    position: relative;
    text-decoration: none;
}
.tree li.parent > a {
    padding: 0 0 0 28px;
}
.tree li.parent > a:before {
    background-position: 25px center;
    content: "-"; 
    display: block;
    height: 21px;
    left: 0;
    position: absolute;
    top: 2px;
    vertical-align: middle;
    width: 23px;
}
.tree ul li.active > a:before {
    background-position: 0 center;
}
.tree ul li ul {
    border-left: 1px solid #D9DADB;
    display: none;
    margin: 0 0 0 12px;
    overflow: hidden;
    padding: 0 0 0 25px;
}
.tree ul li ul li {
    position: relative;
}
.tree ul li ul li:before {
    border-bottom: 1px dashed #E2E2E3;
   
    left: -20px;
    position: absolute;
    top: 12px;
    width: 15px;
}
#wrapper {
    margin: 0 auto;
    width: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="wrapper">
<div class="tree">
<button id="all">Toggle All</button>
		
<ul>
		<li><a>First Level</a>
			<ul>
				<li><a>Second Level</a></li>
				<li><a>Second Level</a></li>
				<li><a>Second Level</a></li>
			</ul>
		</li>
		<li><a>First Level</a>
			<ul>
				<li><a>Second Level</a>
					<ul>
						<li><a>Third Level</a></li>
						<li><a>Third Level</a></li>
						<li><a>Third Level</a>
							<ul>
								<li><a>Fourth Level</a></li>
								<li><a>Fourth Level</a></li>
								<li><a>Fourth Level</a>
									<ul>
										<li><a>Fifth Level</a></li>
										<li><a>Fifth Level</a></li>
										<li><a>Fifth Level</a></li>
									</ul>
								</li>
							</ul>
						</li>
					</ul>
				</li>
				<li><a>Second Level</a></li>
			</ul>
		</li>
		<li><a>First Level</a>
			<ul>
				<li><a>Second Level</a></li>
				<li><a>Second Level</a></li>
			</ul>
		</li>
</ul>
</div>
</div>

【讨论】:

  • 虽然这种方法效果很好,但尽量不重写所有与引导兼容的折叠并重做我的活动方法,希望有一种使用引导后端折叠的简单方法......
  • 另外我已经这样做了,我需要在同一级别上实现其他元素的折叠,所以如果我选择另一个 First Level 它将折叠 Active 元素。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 2012-02-25
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多