递归 JS 函数列出 Hx HTML 标签?
我有一些代码:
$('section').each(function() {
var the_id;
var list_item;
$(this).find('h2').each(function() {
the_id = $(this).attr('id');
$('nav > ol').append('<li><a href="#' + the_id + '">' + $(this).text() + '</a></li>');
$(this).siblings('h3').each(function(i) {
the_id = $(this).attr('id');
if (i == 0) {
$('nav > ol > li:last').append('<ol></ol>');
}
$('nav > ol > li:last > ol').append('<li><a href="#' + the_id + '">' + $(this).text() + '</a></li>');
});
});
});
它会生成一些 HTML,例如:
<li>
<a href="#example-1">Example 1</a>
<ol>
<li>
<a href="#example-1a">Example 1a</a>
</li>
<li>
<a href="#example-1b">Example 1b</a>
</li>
</ol>
</li>
<li>
<a href="#another-example">Another Example</a>
</li>
<li>
<a href="#last-one">Last One</a>
</li>
<li>
<a href="#just-kidding--another">Just kidding, another</a>
<ol>
<li>
<a href="#this-is-a-sub-header">This is a sub header</a>
</li>
</ol>
</li>
我的问题是,我的 JS 只执行我编写的内容(h2,然后查找 h3s,并且我必须为 另一个 编写回调做 h4 然后再做 h5 和 h6 我如何编写一个递归函数来执行此操作?
I have some code:
$('section').each(function() {
var the_id;
var list_item;
$(this).find('h2').each(function() {
the_id = $(this).attr('id');
$('nav > ol').append('<li><a href="#' + the_id + '">' + $(this).text() + '</a></li>');
$(this).siblings('h3').each(function(i) {
the_id = $(this).attr('id');
if (i == 0) {
$('nav > ol > li:last').append('<ol></ol>');
}
$('nav > ol > li:last > ol').append('<li><a href="#' + the_id + '">' + $(this).text() + '</a></li>');
});
});
});
It generates some HTML like:
<li>
<a href="#example-1">Example 1</a>
<ol>
<li>
<a href="#example-1a">Example 1a</a>
</li>
<li>
<a href="#example-1b">Example 1b</a>
</li>
</ol>
</li>
<li>
<a href="#another-example">Another Example</a>
</li>
<li>
<a href="#last-one">Last One</a>
</li>
<li>
<a href="#just-kidding--another">Just kidding, another</a>
<ol>
<li>
<a href="#this-is-a-sub-header">This is a sub header</a>
</li>
</ol>
</li>
My issue is, my JS only goes as far as i write it (h2, then looks for h3s, and i'd have to write another callback for doing h4's then another for h5's and h6's. How can I write a recursive function that does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一种构建 HTML 列表的方法怎么样?我真的不擅长编写/理解递归代码。迭代通常对我来说更容易。
我在您的页面上运行了它,它复制了您现有的目录。而且您不需要为每个级别编写自定义代码;又快又脏。
How about another approach to building the list HTML? I really suck at writing/understanding recursive code. Iteration is usually easier for me.
I ran that on your page and it duplicated your existing TOC. And you don't need custom code for each level; Quick and dirty.
稍微盯着这个,我想我明白你要做什么了。每个级别实际上并没有不同,您只是解释了获得全部内容的最低级别。就像马特的评论所说,您需要单独声明回调。但您还需要将当前级别的列表传递到其中,以便您可以附加到正确的位置。我们可以尝试一种使用构造回调方法的方法,如下所示:
我不确定我是否正确实现了您想要做的事情,但想法是您可以继续创建子列表并将它们传回递归地进入函数。你最初的问题之一是你每次都必须让你的 jQuery 选择器更深。这种方法解决了这个问题。您可以简单地称呼它:
显然您的代码是从使用
$(this).find('h2')
而不是$(this).siblings('h2')
开始的,因此该区域需要进行一些调整。但我确信这没什么大不了的。我没有对此进行测试,所以我可能在某个地方至少犯了一个错误。
A bit of staring at this and I think I figured out what you're going for. Each level isn't actually different, you've just explained the minimum in levels to get it all. Like Matt's comment says, you need to declare the callbacks separately. But you also need to pass your current level of list into it, so you can append to the right place. We can try an approach that uses a method for constructing the callbacks, like this:
I'm not sure if I implemented the thing you're trying to do correctly, but the idea is that you can keep creating sub lists and pass them back into the function recursively. One of your original problems is that you have to make your jQuery selector deeper each time. This approach fixes that. You call it simply:
Obviously your code starts out by using
$(this).find('h2')
instead of$(this).siblings('h2')
, so some adjustment is needed in that area. But I'm sure that's not a big deal.I didn't test this, so I probably made at least one mistake somewhere.
快速 Google 搜索显示此脚本;它是普通的 'ole javascript,但您应该能够根据您的需要调整它(或者,只是借用这个想法并编写您自己的代码)。在尝试了一些之后,我认为 Juan 是对的:迭代方法似乎更容易。我组装了一个使用类似方法的快速 jQuery 插件:
您可以像这样使用它:
其中
toc
是链接所在容器的 ID。A quick Google search revealed this script; it's plain 'ole javascript, but you should be able to adapt it to your needs (or, just borrow the idea and write your own code).After playing around with this some, I think Juan is right: the iterative approach seems easier. I put together a quick jQuery plugin that uses a similar approach:
You'd use it like so:
Where
toc
is the ID of the container where the links should go.