gpt4 book ai didi

javascript - 从列表到可折叠菜单树

转载 作者:行者123 更新时间:2023-11-28 00:32:05 25 4
gpt4 key购买 nike

我正在尝试从 ul 和 li 嵌套元素的列表中制作一个可折叠/可展开的菜单树。

类为“section”的元素只是文件夹,而类为“documents”的元素是文件。

我尝试了 4 或 5 种来自互联网的不同方法,包括纯 CSS 和 jQuery,但我无法使它们都起作用。

现在我正在尝试使用这个脚本:https://www.jqueryscript.net/layout/Animated-Tree-View-Plugin-For-jQuery-Bootstrap-3-MultiNestedLists.html

可悲的是我没有更多的运气,正如你在这个 JSFiddle 上看到的那样

你知道我哪里做错了吗?可能使用了错误的选择器?

非常感谢您的帮助。

顺便说一句,这部分可以在我的电脑上运行,但它似乎无法在 JSFiddle 上运行:

jQuery(document).ready(function() {
jQuery('li').each(function() {
var $this = jQuery(this);
if($this.html().replace(/\s| /g, '').length == 0) {
$this.remove();
}
});
});

最佳答案

您的问题出在这部分代码中:

$this.html()

这样你得到的是 html 而不是文本,而且它永远不会是空的。改用:

if(this.textContent.replace(/\s| /g, '').length == 0) {
jQuery(this).remove();
}

您更新的 fiddle

相反,如果您需要绘制一个文件夹/文件,只需将 multi-nested-list 类添加到第一个 ul 就足够了,没有 js 代码,如您在以下代码段中所见。

请记住:在第一个 ul 之后,所有接下来的都需要包装成一个 li。我只翻译了您的 html 的第一部分。

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/olivsamuk/MultiNestedLists@master/style.css">
<script src="https://cdn.jsdelivr.net/gh/olivsamuk/MultiNestedLists@master/MultiNestedList.js"></script>


<div class="table">
<ul class="multi-nested-list">
<li><a>Preface, revisions, and safety</a>
<ul class="section">
<li><a>Preface</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">Service documentation</a></li>
<li><a class="document">About this documentation</a></li>
<li><a class="document">About this product</a></li>
<li><a class="document">Training</a></li>
<li><a class="document">Trademarks</a></li>
<li><a class="document">Contact</a></li>
<li><a class="document">Feedback</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<ul class="section">
<li><a>Revisions</a>
<ul class="section">
<li><a>Service documentation</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">Version 1.0</a></li>
</ul>
</li>
</ul>
</li>
<ul class="section">
<li><a>Revision history</a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">Version 0.0</a></li>
</ul>
</li>
</ul>
</ul>
</li>
</ul>
<ul class="section">
<li><a>Safety</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">Introduction</a></li>
<li><a class="document">About safety classifications</a></li>
<li><a class="document">About additional information</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<ul class="section">
<li><a>List of WARNING messages</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">About biohazardous material</a></li>
<li><a class="document">About chemical reaction producing toxic gas</a></li>
<li><a class="document">About class 2 laser radiation</a></li>
<li><a class="document">About class 2M laser radiation</a></li>
<li><a class="document">About class 3B laser radiation</a></li>
<li><a class="document">About consumables</a></li>
<li><a class="document">About disposal of instruments, control units or parts</a></li>
<li><a class="document">About electric ground</a></li>
<li><a class="document">About electrical hazards</a></li>
<li><a class="document">About electromagnetic interference</a></li>
<li><a class="document">About environmental conditions</a></li>
<li><a class="document">About fire and burns</a></li>
<li><a class="document">About moving parts</a></li>
<li><a class="document">About optical or ultra violet radiation</a></li>
<li><a class="document">About power interruption</a></li>
<li><a class="document">About reagents and working solutions</a></li>
<li><a class="document">About sharp objects</a></li>
<li><a class="document">About sharp points</a></li>
<li><a class="document">About spare parts</a></li>
<li><a class="document">About strong magnetic field</a></li>
<li><a class="document">About the control unit</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="section">
<li><a>List of CAUTION messages</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a>
<li><a class="document">About bridged lock sensors</a></li>
<li><a class="document">About heavy objects</a></li>
<li><a class="document">About hot surfaces</a></li>
<li><a class="document">About slippery surfaces due to condensation</a></li>
<li><a class="document">About software and data security</a></li>
<li><a class="document">About system cleaner</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<ul class="section">
<li><a>List of NOTICE messages</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">About circuit breakers and fuses</a></li>
<li><a class="document">About electrostatic sensitive devices (ESD)</a></li>
<li><a class="document">About sprays</a></li>
<li><a class="document">About system cleaner replacement</a></li>
<li><a class="document">About wireless interference</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="section">
<li><a>About decontamination procedure prior to shipment</a>
<ul class="section">
<li><a> </a>
<ul class="section">
<li><a> </a></li>
<li><a class="document">About decontamination procedure prior to shipment</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</ul>
</ul>
</div>

关于javascript - 从列表到可折叠菜单树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54347590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com