我想折叠具有多层次结构的类别树。我从堆栈中尝试了很多答案,但是没有用。谁能帮我吗?
我尝试了以下代码:https://stackoverflow.com/a/30945775/7727479
实际结果:
更新:
代码段:
$(document).ready(function() {
var getChild = $('ul.categories').children('li');
getChild.each(function(i, v) {
if ($(v).data('parentcategory') == "0") {
$(v).addClass('active-collapse');
}
});
$('div.categories-list li').click(function() {
var main_category = $(this).data('parentcategory');
$('ul.sub', $(this).parent()).eq(0).toggle();
});
});
div.categories-list {
width: 30%;
margin-bottom: 20px;
float: left;
}
ul.categories,
ul.sub {
border-top: 1px solid #c3cfd9;
margin: 0;
padding: 0;
}
ul.categories li {
margin: 0;
padding: 0;
list-style: none;
}
ul.categories li a {
display: block;
max-width: 100%;
padding: 10px 5px 10px 10px;
font-size: 13px;
background: #FCFCFC;
color: #3f729b;
border: 1px solid #c3cfd9;
border-top: 0px !important;
text-decoration: none;
font-weight: 700;
cursor: pointer;
transition: all 150ms ease-out;
-webkit-transition: all 150ms ease-out;
}
ul.categories li a:hover {
text-indent: 5px;
}
ul.sub {
display: none;
}
.activeUi {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="categories-list">
<ul class="categories">
<li class="child active-collapse" data-parentcategory="0" data-id="1">
<a class="li-child-text" style="padding-left:1%">Test 1</a>
<ul class="sub" data-parentcategory="0" data-id="1">
<li class="child deactive-collapse" data-parentcategory="1" data-id="2">
<a class="li-child-text" style="padding-left:5%">Test 2</a>
<ul class="sub" data-parentcategory="1" data-id="2">
<li class="child deactive-collapse" data-parentcategory="2" data-id="4"><a class="li-child-text" style="padding-left:10%">Test 4</a></li>
<li class="single deactive-collapse" data-parentcategory="4" data-id="5"><a class="li-single-text" style="padding-left:20%">Test 5</a></li>
<li class="single deactive-collapse" data-parentcategory="4" data-id="6"><a class="li-single-text" style="padding-left:20%">Test 6</a></li>
</ul>
</li>
<li class="single deactive-collapse" data-parentcategory="2" data-id="7"><a class="li-single-text" style="padding-left:10%">Test 7</a></li>
</ul>
<ul class="sub" data-parentcategory="0" data-id="1">
<li class="child deactive-collapse" data-parentcategory="1" data-id="8"><a class="li-child-text" style="padding-left:5%">Test 8</a></li>
<li class="single deactive-collapse" data-parentcategory="8" data-id="9"><a class="li-single-text" style="padding-left:40%">Test 9</a></li>
</ul>
</li>
<li class="single active-collapse" data-parentcategory="0" data-id="3"><a class="li-single-text" style="padding-left:1%">Test 3</a></li>
</ul>
</div>
答案 0 :(得分:1)
我认为主要问题在于您的html结构,如果要创建一个级别,则需要在该级别中创建li
,并在anchor
元素中添加li
。 / p>
例如,要在 TEST 1.0 中添加 TEST 2.2 ,您需要在用于 TEST 1.0的anchor
之后找到下一个ul.sub < / strong>并使用li
添加另一个anchor
,如果要在 TEST2.2 中创建另一个子菜单,则需要使用sub和class创建ul
和另一个li
<li class="single deactive-collapse">
<a class="li-single-text" style="padding-left:5%">Test 2.2 LAST</a>
<ul class="sub" >
<li class="child deactive-collapse">
<a class="li-child-text" style="padding-left:10%">Test 3.2 LAST</a>
</li>
</ul>
</li>
这是代码段
var addById = function (parent, id, name) {
$('div.categories-list li').each(function (i, e) {
if (parent === $(e).data("id")) {
if (!$(e).find("ul.sub").length) {
$(e).append('<ul class="sub"></ul>');
}
element = '<li class="child deactive-collapse" data-parentcategory="' + parent + '" data-id="' + id + '"><a class="li-child-text" style="padding-left:5%">' + name + '</a></li>';
$(e).find("ul.sub").append($(element));
return true;
}
});
};
addById(1, 123, 'dynamically');
addById(15, 124, 'dynamically');
$(document).ready(function () {
$('div.categories-list li > a').click(function () {
var main_category = $(this).data('parentcategory');
$('ul.sub', $(this).parent()).eq(0).toggle();
return false;
});
});
div.categories-list {
width: 30%;
margin-bottom: 20px;
float: left;
}
ul.categories,
ul.sub {
border-top: 1px solid #c3cfd9;
margin: 0;
padding: 0;
}
ul.categories li {
margin: 0;
padding: 0;
list-style: none;
}
ul.categories li a {
display: block;
max-width: 100%;
padding: 10px 5px 10px 10px;
font-size: 13px;
background: #FCFCFC;
color: #3f729b;
border: 1px solid #c3cfd9;
border-top: 0px !important;
text-decoration: none;
font-weight: 700;
cursor: pointer;
transition: all 150ms ease-out;
-webkit-transition: all 150ms ease-out;
}
ul.categories li a:hover {
text-indent: 5px;
}
ul.sub {
display: none;
}
.activeUi {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="categories-list">
<ul class="categories">
<li class="child active-collapse" data-parentcategory="0" data-id="1">
<a class="li-child-text" style="padding-left:1%">Test 1.0</a>
<ul class="sub">
<li class="child deactive-collapse" data-parentcategory="1" data-id="2">
<a class="li-child-text" style="padding-left:5%">Test 2.0</a>
<ul class="sub">
<li class="child deactive-collapse" data-parentcategory="2" data-id="4">
<a class="li-child-text" style="padding-left:10%">Test 3.0 LAST</a>
</li>
<li class="single deactive-collapse" data-parentcategory="2" data-id="5">
<a class="li-single-text" style="padding-left:10%">Test 3.0 LAST</a>
</li>
<li class="single deactive-collapse" data-parentcategory="2" data-id="6">
<a class="li-single-text" style="padding-left:10%">Test 3.0 LAST</a>
</li>
</ul>
</li>
<li class="single deactive-collapse" data-parentcategory="1" data-id="7">
<a class="li-single-text" style="padding-left:5%">Test 2.1 LAST</a>
</li>
<li class="child deactive-collapse" data-parentcategory="1" data-id="8">
<a class="li-child-text" style="padding-left:5%">Test 8</a>
<ul class="sub" data-parentcategory="8" data-id="9">
<li class="single deactive-collapse" data-parentcategory="8" data-id="9">
<a class="li-single-text" style="padding-left:40%">Test 9</a>
</li>
</ul>
</li>
<li class="single deactive-collapse" data-parentcategory="1" data-id="10">
<a class="li-single-text" style="padding-left:5%">Test 2.2 LAST</a>
<ul class="sub">
<li class="child deactive-collapse" data-parentcategory="10" data-id="11">
<a class="li-child-text" style="padding-left:10%">Test 3.2 LAST</a>
</li>
</ul>
</li>
<!-- PUT EXAMPLE CODE HERE -->
</ul>
</li>
<li class="child active-collapse" data-parentcategory="0" data-id="12">
<a class="li-child-text" style="padding-left:1%">Test 1.1</a>
<ul class="sub">
<li class="child deactive-collapse" data-parentcategory="12" data-id="13"><a class="li-child-text" style="padding-left:5%">Test 2.1 LAST</a></li>
<li class="single deactive-collapse" data-parentcategory="12" data-id="14"><a class="li-single-text" style="padding-left:5%">Test 2.1 LAST</a></li>
</ul>
</li>
<li class="single active-collapse" data-parentcategory="0" data-id="15"><a class="li-single-text" style="padding-left:1%">Test 1.2 LAST</a></li>
</ul>
</div>
答案 1 :(得分:1)
是的,您对ul li的结构有一些疑问。请看一下我的演示。您可以将类和属性添加到您的遗嘱中。不会妨碍。
document.querySelector('.categories').addEventListener('click', (e) => {
const el = e.target;
const sibling = el.nextSibling.nextSibling;
if (el && el.className == 'toggle' && sibling) {
sibling.classList.toggle('show');
}
});
div.categories-list {
width: 50%;
margin-bottom: 20px;
float: left;
}
ul.categories,
ul.sub {
border-top: 1px solid #c3cfd9;
margin: 0;
padding: 0;
}
ul.categories li {
margin: 0;
padding: 0;
list-style: none;
}
ul.categories li a {
display: block;
max-width: 100%;
padding: 10px 5px 10px 10px;
font-size: 13px;
background: #FCFCFC;
color: #3f729b;
border: 1px solid #c3cfd9;
border-top: 0px !important;
text-decoration: none;
font-weight: 700;
cursor: pointer;
transition: all 150ms ease-out;
-webkit-transition: all 150ms ease-out;
}
ul.categories li a:hover {
text-indent: 5px;
}
ul.sub {
display: none;
}
.activeUi {
display: block;
}
ul ul {
display: none;
}
.show {
display: block;
}
<div class="categories-list">
<ul class="categories">
<li >
<a href="javascript:;" class="toggle">Test 1</a>
<ul>
<li>
<a href="javascript:;" class="toggle">Test 2</a>
<ul>
<li>
<a href="javascript:;" class="toggle">Test 4</a>
<ul>
<li>
<a href="javascript:;" class="toggle">Test 5</a>
</li>
<li>
<a href="javascript:;" class="toggle">Test 6</a>
</li>
</ul>
</li>
<li>
<a href="javascript:;" class="toggle">Test 7</a>
</li>
</ul>
</li>
<li>
<a href="javascript:;" class="toggle">Test 8</a>
<ul>
<li>
<a href="javascript:;" class="toggle">Test 9</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="javascript:;" class="toggle">Test 3</a>
</li>
</ul>
</div>