WP : 验证器中的杂散开始标记 HTML


WP : Stray start tag HTML in validator?

我正在尝试用 http://validator.w3.org/#validate_by_input 验证此HTML文档,但出现以下错误:

 Line 10, Column 71: Stray start tag html.
<html dir="rtl" lang="ar" prefix="og: http://ogp.me/ns#" class="no-js">

这是我输入的 HTML:

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--[if IE 7]><html <?php language_attributes(); ?> class="ie ie7 no-js" /><![endif]-->
<!--[if IE 8]><html <?php language_attributes(); ?> class="ie ie8 no-js" /><![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!--><html <?php language_attributes(); ?> class="no-js" /><!--<![endif]-->
<head>

这里的 <meta> 标签意味着前面有一个额外的<html><head>标签。

根据HTML5标准,一些标签是可选的。 <html><head>就是这样的标签。您可以删除它们,但解析器会在需要时插入它们。在您的情况下,它们需要在 <meta> 标记之前,因为<meta>被定义为 <head> 标记的子元素,而 标记又是 <html> 的子元素。

所以验证者实际看到的是这样的:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--[if IE 7]><html <?php language_attributes(); ?> class="ie ie7 no-js" /><![endif]-->
<!--[if IE 8]><html <?php language_attributes(); ?> class="ie ie8 no-js" /><![endif]-->
<!--[if !(IE 7) | !(IE 8)  ]><!--><html <?php language_attributes(); ?> class="no-js" /><!--<![endif]-->
<head>

现在很明显,为什么它抱怨"第二个"<html>标签。

解决方案是仅更改标签的顺序。将<html><head>标签放在<meta>标签之前,您应该没问题。