元素样式不允许作为此上下文中元素主体的子元素(< style scoped> not validating) [英] Element style not allowed as child of element body in this context (<style scoped> not validating)

查看:139
本文介绍了元素样式不允许作为此上下文中元素主体的子元素(< style scoped> not validating)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

w3.org验证器给我这个错误:

 第883行第17列:元素样式不允许作为此上下文中元素主体的子元素。 
(从该子树抑制进一步的错误。)
将风格作用域> ...
上下文,其中元件样式可以使用:
。如果所述作用域属性不存在:其中元数据内容是预期的。
如果scoped属性不存在:位于head元素的子元素的noscript元素中。
如果存在scoped特性:预期存在流内容,但在除元素间空白和样式元素以外的任何其他流内容之前,而不是其内容模型是透明的元素的子元素。
元素主体的内容模型:
流内容。

据我的理解,'scoped'属性可以让style标签在文件的头。那么,为什么验证器不满意它?



(我使用的是Wordpress,这个代码是由一个插件生成的,这就是为什么我不能把它放在)



编辑:
这不验证 -

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

但是,如果脚本标签位于样式标签之后。这是什么原因?

解决方案W3C标记验证程序在作为HTML5检查程序时,根据各种方法处理此问题草稿,例如 HTML 5.1 Nightly ,它现在说< style 元素可能只出现在元素中,除非存在 scoped 属性,在这种情况下,它可能会出现流内容预计在哪里,但在元素之间的空白和样式元素以外的任何其他流内容之前,而不是其内容模型是透明的元素的子元素。在您的实际示例中,元素出现在脚本元素之后(它被计为流内容)。在给定的定义下,更改元素的顺序因此将语法更改为有效。



或者,您可以包装样式 div 元素中的c $ c>元素:

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

scoped 属性根本无效根据W3C建议 HTML5 。它出现在HTML5草案中,但由于缺乏实现,它被从推荐中删除,但它仍然处于标准化轨道,并可能向HTML 5.1发展。



请注意,现有的浏览器通常会忽略 scoped 属性,并且几乎允许在任何地方使用样式元素并应用其内容到整个HTML文档(甚至是在样式元素之前的部分)。


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

The w3.org validator is giving me this error:

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. 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.)

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". In your actual example, the element appears after a script element (which is counted as flow content). 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:

<!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. 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.

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).

这篇关于元素样式不允许作为此上下文中元素主体的子元素(&lt; style scoped&gt; not validating)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆