哪个 javascript 库或框架支持“目录”? 一代?
我正在寻找一个从 HTML(带有锚点)动态生成“目录”的 javascript。
示例:
<h1>First level1 heading</h1>
lorem ipsum
<h2>1a heading</h2>
lorem ipsum
<h2>1b heading</h2>
lorem ipsum
<h1>Second level1 heading</h1>
lorem ipsum
应该返回类似于
First level1 heading
1a heading
1b heading
Second level1 heading
链接到标题的行的内容,并且应该返回插入锚点的原始 html。
大型 javascript 库或框架之一中是否包含某些内容?
如果没有,有人见过用于此目的的良好 JS 模块吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 是你的朋友,有这个插件:目录。 首页是 http://code.google.com/p/samaxesjs/
jQuery is your friend, with this plugin: table of contents. Home page is http://code.google.com/p/samaxesjs/
自己制作,我写的:),希望它有助于
添加一个 div 元素作为 body 元素的第一个子元素,并给出一个 id 作为“tableOfContents”
,并将下面的脚本添加为 body 元素的最后一个子元素,
该脚本将检测每个 H (1到 5) 并生成目录
Make it yourself, i wrote it :), hope it helps
add a div element as first child of body element and give an id as "tableOfContents"
and add the script below as last child of body element
this script will detects each H (1 to 5) and generates your table of contents
这是一个非常简单的问题,可以用 10-20 行函数来解决。 无需框架。 使用 getElementsByTagName('h1')、getElementsByTagName('h2') 遍历 DOM 或使用正则表达式。 加载框架会带来性能影响和风险,因此我建议不要为简单的问题安装框架。
This is a very simple problem that could be solved with a 10-20 line function. No framework required. Either walk the DOM with getElementsByTagName('h1'), getElementsByTagName('h2') or use regular expressions. Loading frameworks comes with performance implications and risks so I suggest not installing one for simple problems.