有没有一个网页控件可以动态生成目录?
假设我有一个像这样的基本页面:
<custom:TableOfContents />
<h1>Some Heading</h1>
<h2>Foo</h2>
<p>Lorem ipsum</p>
<h2>Bar</h2>
<p>Lorem ipsum</p>
<h2>Baz</h2>
<p>Lorem ipsum</p>
<h1>Another Heading</h2>
<h2>Qux</h2>
<p>Lorem ipsum</p>
<h2>Quux</h2>
<p>Lorem ipsum</p>
假设所有标题标记都作为服务器端控件存在。是否有一些适用于 ASP.NET Web 表单的 Web 控件
可以动态生成如下内容的目录(当呈现到屏幕上时):
1. Some Heading
1.1. Foo
1.2. Bar
1.3. Baz
2. Another Heading
2.1. Qux
2.2. Quux
理想情况下,每个目录中的条目将是指向页面上适当位置动态生成的锚点的超链接。另外,如果每个标题标签的文本都可以以其节号作为前缀,那就太好了。
如果不是网络控件,是否有一些更简单的方法可以做到这一点?请记住,许多标题标记将由数据绑定控件创建,因此手动维护目录不是一种选择。看起来 Webforms 模型非常适合创建这样的控件,这就是为什么我很惊讶我还没有找到一个控件。
Say I have a basic page like so:
<custom:TableOfContents />
<h1>Some Heading</h1>
<h2>Foo</h2>
<p>Lorem ipsum</p>
<h2>Bar</h2>
<p>Lorem ipsum</p>
<h2>Baz</h2>
<p>Lorem ipsum</p>
<h1>Another Heading</h2>
<h2>Qux</h2>
<p>Lorem ipsum</p>
<h2>Quux</h2>
<p>Lorem ipsum</p>
Assume all the header tags exist as server side controls. Is there some web control <custom:TableOfContents />
for ASP.NET webforms that will dynamically generate a table of contents that looks something like the following (when rendered to the screen):
1. Some Heading
1.1. Foo
1.2. Bar
1.3. Baz
2. Another Heading
2.1. Qux
2.2. Quux
Ideally, each entry in the table of contents would be a hyperlink to a dynamically generated anchor at the appropriate place on the page. Also, it would be nice if the text of each header tag could be prefixed with its section number.
If not a web control, is there some easier way of doing this? Keep in mind that many of the header tags are going to be created by data bound controls, so manually maintaining the table of contents is not an option. It seems like the webforms model is ideally suited to creating such a control, which is why I'm surprised I haven't yet found one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几天前我需要做类似的事情,虽然不是 webcontrol,但使用了 jQuery。
这将生成一个有序列表,并使用适当的编号(例如 1.1)将其附加到
#table-of-contents
。只需要一点点 CSS 就可以隐藏列表的内置编号:#table-of-contents ol { list-style:none; }。I needed to do a similar thing a few days ago and, though not a webcontrol, used jQuery.
This will make an ordered list and append it to
#table-of-contents
with appropriate numbering (e.g., 1.1). Just a little bit of CSS is needed to hide the lists' built in numbering:#table-of-contents ol { list-style:none; }
.