【发布时间】:2018-04-27 23:40:48
【问题描述】:
原谅我有限的 js 经验。我继承了一些旧的、基于框架集的软件在线文档,我需要更新到 HTML5/responsive。作者将每个“主题”输入到基于桌面的 CMS 中,然后 CMS 将每个主题“发布”到 html 页面。除了主题页面,它还发布了一个我加载并使用的 table-of-contents.htm (TOC),以使用户能够选择和加载所需的主题。
主题按“书籍”或父主题分组,形成树状目录。默认情况下,所有“书籍”都是折叠的。我需要能够展开/折叠 TOC 条目,以便当用户选择一个主题时,该主题及其直接父主题被展开。这是系统为我生成的 toc.htm 的一个子集。我无法控制这个生成的文件:
<!-- excerpt from generated Table Of Contents -->
<h1 class="heading1">Contents</h1>
<table class="tabledhtmltoc" cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td align="left" valign="top" nowrap="nowrap"><span class="toc"> <br>
<img id="p145054" src="plus.gif" onclick="exp('145054')" alt=""><img id="b145054" src="cbook.gif" alt=""><a id="a145054" href="145054.htm">Data Optimization Online Help</a><br>
<span id="s145054" class="sp"><img style="margin-left:16px" id="p90584" src="plus.gif" onclick="exp('90584')" alt=""><img id="b90584" src="cbook.gif" alt=""><a id="a90584" href="xxoxxrview.htm">Corporate Enterprise oxxrview</a><br>
<span id="s90584" class="sp"><img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a98605" href="98605.htm">The Home page - Corporate Enterprise</a><br>
<img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a84599" href="logging_on_xx.htm">Logging on to the Corporate Enterprise Home page</a><br>
<img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a134150" href="134150.htm">Logging off of Corporate Enterprise</a><br>
<img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a100102" href="accessing_xx_solutions.htm">Accessing a Corporate Enterprise application</a><br>
</span>
<img style="margin-left:16px" id="p144468" src="plus.gif" onclick="exp('144468')" alt=""><img id="b144468" src="cbook.gif" alt=""><a id="a144468" href="144468.htm">About the System user interface</a>
<br>
<span id="s144468" class="sp"><img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a142049" href="142049.htm">Drawers</a><br>
<img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a142437" href="142437.htm">View columns</a><br>
<img style="margin-left:32px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a143110" href="143110.htm">Filter values in columns</a><br>
<img style="margin-left:32px" id="p143373" src="plus.gif" onclick="exp('143373')" alt=""><img id="b143373" src="cbook.gif" alt=""><a id="a143373" href="usinghelp_xx.htm">Using the online help</a><br>
<span id="s143373" class="sp"><img style="margin-left:48px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a143352" href="accessing_help_xx.htm">Accessing help</a><br>
<img style="margin-left:48px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a143353" href="finding_topics_with_contents.htm">Finding topics with Contents</a><br>
<img style="margin-left:48px" src="space.gif" alt=""><img src="topic.gif" alt=""><a id="a143354" href="finding_topics_with_search.htm">Finding topics with Search</a><br>
</span></span>
<!-- entries continue on for depending on how many topics/nodes there are in total -->
</td>
</tr>
</table>
在每个单独的主题页面中,都有一个 onload 引用主题的 id 和它的父主题:
<body onload="if (isTOCLoaded()) {expand('144468');expand('145054');highlight('142049')}">
我有 jquery 并且更喜欢使用它,因为我在这个项目中将它用于许多其他功能,但如果它更有意义的话,我可以使用 vanilla javascript。
我将如何编写这样的函数:
function expand(id) { // this is called for each id via the body onload
// expand the id (topic) in the toc...
}
非常感谢任何帮助!我在这上面花了几个小时:(
【问题讨论】:
-
有活生生的例子吗?
-
所以我们要切换左侧的菜单树?
-
不只是切换所有的展开/折叠——我能够弄清楚(通过简单地将“sp”类的显示设置为无)。我需要做的是:当用户单击菜单树中的链接时,页面会在右侧加载,然后整个树会折叠,除了加载的主题及其父主题(以及所有兄弟主题)。
标签: javascript jquery