英文:
Optimizing a nested list with folding options
问题
我的问题基于这个主题,但我想要添加一些功能。
当前的代码如下所示:
$(document).ready(function() {
$('ol ol').hide();
$('li').click(function() {
$(this).children('ol').toggle();
});
});
* {
margin: 0;
padding: 0;
font-family: Arial;
cursor: default;
}
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".") " ";
/* content: counters(item, ".", decimal-leading-zero) " "; */
counter-increment: item;
}
li:has(ol):before {
float: left;
/* margin-bottom: 5px; */
}
ol ol {
overflow: hidden;
}
.active {
color: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
</ol>
需要以下功能:
- 如果单击
Coffee
或Tea
,下属菜单项应该展开,但从左侧开始的位置应该紧贴在单词下方,而不是数字,方式如下解决这里。 - 一次只能打开一个菜单项。
- 最后单击的菜单项应该切换
.active
类。 - 如果菜单项多于9个,所有菜单项应该在各自数字前面加上0,以确保所有内容仍然在适当的位置。
这是一个示例,看起来可能是这样的:
有人知道如何做到这一点吗?我将非常感谢您的帮助。
英文:
My question is based on this topic, but I would like to add a few functions.
The current code looks like that:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('ol ol').hide();
$('li').click(function() {
$(this).children('ol').toggle();
});
});
<!-- language: lang-css -->
* {
margin: 0;
padding: 0;
font-family: Arial;
cursor: default;
}
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".") " ";
/* content: counters(item, ".", decimal-leading-zero) " "; */
counter-increment: item;
}
li:has(ol):before {
float: left;
/* margin-bottom: 5px; */
}
ol ol {
overflow: hidden;
}
.active {
color: green;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
</ol>
<!-- end snippet -->
The following is needed:
- If you click
Coffee
orTea
, the the subordinate menu items
should expand, but with a space from the left starting just below
the word and not the digit, the way it was solved here. - It should only be possible to open one menu item at a time.
- The menu item clicked last should toggle the
.active
class. - If there are more than 9 menu items, all menu items should start
with a 0 in front of the respective digit so that everything is
still on the fitting position.
Here is an example how it can look like:
Does someone know how to do that? I would be very grateful for help.
答案1
得分: 0
以下是翻译好的内容,代码部分未翻译:
我会切换一个类并在 CSS 中处理所有逻辑,如下所示
$(document).ready(function() {
$('li').click(function() {
$('ol.active').removeClass("active");
$(this).children('ol').addClass("active");
});
});
* {
margin: 0;
padding: 0;
font-family: Arial;
cursor: default;
}
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".") " ";
counter-increment: item;
}
/* 如果超过10,则添加前导零 */
li:first-child:nth-last-child(n + 10):before,
li:first-child:nth-last-child(n + 10) ~ li:before {
content: counters(item, ".", decimal-leading-zero) " ";
}
/* 选择具有 "ol" 的 li */
li:has(ol.active):before {
float: left;
margin-bottom: 5px; /* 较小的值 */
}
ol ol {
overflow: hidden;
display: none;
}
ol ol.active {
color: green;
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
</ol>
<hr>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
</ol>
希望这有帮助!
英文:
I would toggle a class and consider all the logic inside the CSS like below
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('li').click(function() {
$('ol.active').removeClass("active");
$(this).children('ol').addClass("active");
});
});
<!-- language: lang-css -->
* {
margin: 0;
padding: 0;
font-family: Arial;
cursor: default;
}
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, ".") " ";
counter-increment: item;
}
/* add leading 0 if more than 10*/
li:first-child:nth-last-child(n + 10):before,
li:first-child:nth-last-child(n + 10) ~ li:before{
content: counters(item, ".", decimal-leading-zero) " ";
}
/* select li that has "ol" */
li:has(ol.active):before {
float: left;
margin-bottom: 5px; /* small value */
}
ol ol {
overflow: hidden;
display: none;
}
ol ol.active {
color: green;
display: block;
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
</ol>
<hr>
<ol>
<li>Coffee
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Tea
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
</li>
<li>Milk</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
<li>Cool</li>
</ol>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论