前言
a标签按照以往的标准,他属于内联元素,不应该包含块级元素,但是他又是可以包含的, 这是为什么呢?
按新的 HTML 规范,已经不按 inline 和 block 来区分元素类型了。
原因
在规范中每个元素会规定如下两项:
- Categories
-
Content Model
Categories 是改元素本身的分类,content model 规定了合法的元素的内容(子元素、文本等)类型。
<meta charset="utf-8">
a 元素的 content model 为 transparent。
Some elements are described as transparent; they have "transparent" in the description of their content model. The content model of a transparent element is derived from the content model of its parent element: the elements required in the part of the content model that is "transparent" are the same elements as required in the part of the content model of the parent of the transparent element in which the transparent element finds itself.
大意就是这类元素本身内部可以有任何类型的内容,是否合法要看其父元素的 content model 和其内容的 categories。
比如我们要看 p > ins > a > div 是否合法,过程是这样的:
- p 元素的 content model 是 phrasing content,ins 本身属于 phrasing content 故可以嵌套;
- ins 元素的 content model 是 transparent,故在此时里面是否能有 a 需检查 p > a 的合法性;
- a 元素也属于 phrasing content,故 p > ins > a 合法;
- a 元素的 content model 也是 transparent,故此时里面包含 div 的合法性向上传递,检查 ins > div 又向上传递,变成检查 p > div;
- div 不属于 phrasing content,所以这个嵌套是不合法的。
总结
对于各种新标准,还是需要了解的
网友评论