gpt4 book ai didi

html - 在 HTML5 中混合显式和隐式分段时不可能的情况?

转载 作者:可可西里 更新时间:2023-11-01 13:03:39 25 4
gpt4 key购买 nike

我找到了 a page about sections and outlines对于 HTML5。不能 100% 确定它是官方的,并且页面上说大多数浏览器不遵循它,但我认为这是一个很酷的想法,所以我正在尝试创建代码来显式插入隐式部分并创建大纲。

我遇到的概念性问题类似于隐式和显式分段混合的示例之一(缩写版):

<body>
<h1>Mammals</h1>
<h2>Whales</h2>
...
<section>
<h3>Forest elephants</h3>
...
<h3>Mongolian gerbils</h3>
...
<h2>Reptiles</h2>
...
</section>
</body>

应该给出:

1. Mammals
1.1 Whales (implicitly defined by the h2 element)
1.2 Forest elephants (explicitly defined by the section element)
1.3 Mongolian gerbils (implicitly defined by the h3 element, which closes the previous section at the same time)
2. Reptiles (implicitly defined by the h2 element, which closes the previous section at the same time)

但是我在这方面遇到了麻烦(开始一个带有标题的部分,然后稍后使用不太深的标题)。考虑这个稍微修改过的例子:

<body>
<h1>Mammals</h1>
<h2>Whales</h2>
...
<section>
<h3>Forest elephants</h3>
...
<h2>Reptiles</h2>
...
<h1>Martians</h2>
<p>Just being annoying</p>
</section>
</body>

我该如何处理?最后<h1>最终应该达到 <h0> 的水平在外部范围内,不存在。

(我个人认为以 <h3> 开头的显式部分只允许 <h3><h4><h5><h6> 最有意义,但这并不能重现示例,我我想以“官方”方式进行。)

最佳答案

大纲算法通过简单地不允许分段元素内的标题元素高于分段元素本身来解决这个问题。来自spec :

When entering a heading content element

If the current section has no heading, let the element being entered be the heading for the current section.

Otherwise, if the element being entered has a rank equal to or higher than the heading of the last section of the outline of the current outline target, or if the heading of the last section of the outline of the current outline target is an implied heading, then create a new section and append it to the outline of the current outline target element, so that this new section is the new last section of that outline. Let current section be that new section. Let the element being entered be the new heading for the current section.

例如,如果您有一个 <h3>作为一个部分的第一个标题,然后是任何 <h2><h1>在同一部分中跟随它的将被平等对待,创建与第一个部分分开的隐式部分。只有<h4><h6>将在不开始新部分的情况下创建隐式小节。

您的第一个示例实际上生成了以下大纲:

1. Mammals   1.1 Whales   1.2 Forest elephants   1.3 Mongolian gerbils   1.4 Reptiles

您的第二个示例生成以下大纲:

1. Mammals   1.1 Whales   1.2 Forest elephants   1.3 Reptiles   1.4 Martians

关于html - 在 HTML5 中混合显式和隐式分段时不可能的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35918550/

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