1 回答
TA贡献1817条经验 获得超14个赞
您当然必须根据您的需要调整样式和 html:
$('li').on("click", function(e) {
e.stopPropagation();
let $el = $(this);
if(!$el.children('ul').hasClass('active')) {
$('#tree').find('ul').removeClass('active');
$el.parentsUntil('#tree').addClass('active');
}
$el.children('ul').toggleClass('active');
});
ul {
list-style-type: none;
display:none;
}
#tree {
display:block;
}
li {
cursor: pointer;
background: #F1F1F1;
padding: 5px;
border: 1px solid #CECECE;
}
.active {
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="tree">
<li>EU
<ul>
<li>FR
<ul>
<li>Division 1</li>
<li>Division 2</li>
</ul>
</li>
<li>NL
<ul>
<li>Division 1</li>
<li>Division 2</li>
</ul>
</li>
</ul>
</li>
<li>US
<ul>
<li>TX
<ul>
<li>Division 1</li>
<li>Division 2</li>
</ul>
</li>
<li>NY
<ul>
<li>Division 1</li>
<li>Division 2</li>
</ul>
</li>
</ul>
</li>
</ul>
添加回答
举报