简体   繁体   English

在此上下文中不允许元素样式作为元素主体的子元素( <style scoped> not validating)

[英]Element style not allowed as child of element body in this context (<style scoped> not validating)

<!DOCTYPE html>
...
<style scoped>
/* css */
</style>

The w3.org validator is giving me this error: w3.org验证器给了我这个错误:

Line 883, Column 17: Element style not allowed as child of element body in this context.
(Suppressing further errors from this subtree.)
        <style scoped>...
Contexts in which element style may be used:
If the scoped attribute is absent: where metadata content is expected.
If the scoped attribute is absent: in a noscript element that is a child of a head element.
If the scoped attribute is present: where flow content is expected, but before any other flow     content other than inter-element whitespace and style elements, and not as the child of an element whose content model is transparent.
Content model for element body:
Flow content.

It is my understanding that the 'scoped' property makes it ok for the style tag to be outside the head of the document. 我的理解是'scoped'属性使得样式标记可以在文档的头部之外。 So why is the validator unhappy with it? 那么为什么验证者不满意呢?

(I'm using Wordpress and this code is produced by a plugin, which is why I can't just put it in the head.) (我正在使用Wordpress,这个代码是由插件生成的,这就是为什么我不能把它放在头脑中。)

EDIT: This doesn't validate - 编辑:这不验证 -

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<script type="text/javascript"></script>
<style scoped></style>
</body>
</html>

But it does if the script tag goes after the style tag. 但是如果脚本标记在样式标记之后,它就会发生。 What is the reason for that? 这是什么原因?

The W3C Markup Validator, when acting as an HTML5 checker, handles this issue according to various drafts such as HTML 5.1 Nightly , which right now says that the style element may appear only inside the head element, except when the scoped attribute is present, in which case it may appear “where flow content is expected, but before any other flow content other than inter-element whitespace and style elements, and not as the child of an element whose content model is transparent”. W3C标记验证器在充当HTML5检查程序时,会根据各种草稿来处理此问题,例如HTML 5.1 Nightly ,现在说style元素可能只出现在head元素内部,除非存在scoped属性,在哪种情况下,它可能看起来“在预期流内容的地方,但在除了元素间空格和样式元素之外的任何其他流内容之前,而不是作为内容模型是透明的元素的子节点”。 In your actual example, the element appears after a script element (which is counted as flow content). 在您的实际示例中,元素出现在script元素(计为流内容)之后。 Changing the order of the elements thus changes the syntax to valid, under the given definition. 因此,在给定定义下,更改元素的顺序会将语法更改为有效。

Alternatively, you can just wrap the style element in a div element: 或者,您可以将style元素包装在div元素中:

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<script type="text/javascript"></script>
<div>
  <style scoped></style>
</div>
</body>
</html>

The scoped attribute is not valid at all according to the W3C Recommendation HTML5 . 根据W3C Recommendation HTML5scoped属性完全无效。 It was present in HTML5 drafts, but it was dropped from the Recommendation due to lack of implementations, but it is still in the “standardization track” and may make its way to HTML 5.1. 它存在于HTML5草案中,但由于缺乏实现而被从建议书中删除,但它仍处于“标准化轨道”中,可能会进入HTML 5.1。

Note that existing browsers generally ignore the scoped attribute and allow a style element almost anywhere and apply its content to the entire HTML document (even parts that precede the style element). 请注意,现有浏览器通常会忽略scoped属性,并且几乎可以在任何位置使用style元素,并将其内容应用于整个HTML文档(甚至是style元素之前的部分)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下,不允许将元素样式作为元素主体的子元素 - Element style not allowed as child of element body in this context 在这种情况下,不允许将元素样式作为元素主体的子元素。 (抑制此子树中的其他错误。) - Element style not allowed as child of element body in this context. (Suppressing further errors from this subtree.) 流浪开始标签头,在此上下文中不允许元素样式作为元素主体的子元素。 (抑制此子树中的其他错误。) - Stray start tag head, Element style not allowed as child of element body in this context. (Suppressing further errors from this subtree.) 在这种情况下,不允许将li元素作为element body的子元素 - element li not allowed as child of element body in this context 如何修复以下错误:错误:在此上下文中,元素样式不允许作为元素标题的子元素 - How to fix the following error : Error: Element style not allowed as child of element header in this context HTML 验证器错误 - 在此上下文中不允许元素滑块作为元素主体的子元素 - HTML Validator Error - Element slider not allowed as child of element body in this context HTML 元素样式在作用域样式中不起作用 - HTML Element styling not working in scoped style 在这种情况下,元素div不允许作为元素ol的子元素 - Element div not allowed as child of element ol in this context 在这种情况下,不允许将元素管理员作为元素范围的子级 - Element admin not allowed as child of element span in this context 在这种情况下,元素img不允许作为元素图片的子元素 - Element img not allowed as child of element picture in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM