优化具有折叠选项的嵌套列表

huangapple go评论54阅读模式
英文:

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>

需要以下功能:

  1. 如果单击CoffeeTea,下属菜单项应该展开,但从左侧开始的位置应该紧贴在单词下方,而不是数字,方式如下解决这里
  2. 一次只能打开一个菜单项。
  3. 最后单击的菜单项应该切换.active类。
  4. 如果菜单项多于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() {
  $(&#39;ol ol&#39;).hide();

  $(&#39;li&#39;).click(function() {
    $(this).children(&#39;ol&#39;).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, &quot;.&quot;) &quot; &quot;;
  /* content: counters(item, &quot;.&quot;, decimal-leading-zero) &quot; &quot;; */
  counter-increment: item;
}

li:has(ol):before {
  float: left;
  /* margin-bottom: 5px; */
}

ol ol {
  overflow: hidden;
}

.active {
  color: green;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;ol&gt;
  &lt;li&gt;Coffee
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Tea
    &lt;ol&gt;
      &lt;li&gt;One&lt;/li&gt;
      &lt;li&gt;Two&lt;/li&gt;
      &lt;li&gt;Three&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;Milk&lt;/li&gt;
  &lt;li&gt;Cool&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

The following is needed:

  1. If you click Coffee or Tea, 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.
  2. It should only be possible to open one menu item at a time.
  3. The menu item clicked last should toggle the .active class.
  4. 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() {
$(&#39;li&#39;).click(function() {
$(&#39;ol.active&#39;).removeClass(&quot;active&quot;);
$(this).children(&#39;ol&#39;).addClass(&quot;active&quot;);
});
});

<!-- language: lang-css -->

* {
margin: 0;
padding: 0;
font-family: Arial;
cursor: default;
}
ol {
counter-reset: item;
}
li {
display: block;
}
li:before {
content: counters(item, &quot;.&quot;) &quot; &quot;;
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, &quot;.&quot;, decimal-leading-zero) &quot; &quot;;
}
/* select li that has &quot;ol&quot; */
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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;ol&gt;
&lt;li&gt;Coffee
&lt;ol&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Tea
&lt;ol&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Milk&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li&gt;Coffee
&lt;ol&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Tea
&lt;ol&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Milk&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;li&gt;Cool&lt;/li&gt;
&lt;/ol&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月10日 01:49:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定