为样式表创建有效的条件注释,不会出现“虚假注释”验证器错误

2023-12-28

我的 head 标签中有以下内容:

<link rel="stylesheet" href="Scripts/styleMain.css" media="all" type="text/css" />
<![if gte IE 9]><!-->
<link rel="stylesheet" href="Scripts/styleMob.css" media="screen and (max-width:500px)" type="text/css" />
<link rel="stylesheet" href="Scripts/styleDes.css" media="screen and (min-width:501px)" type="text/css" />
<!--<![endif]-->
<!--[if lt IE 9]>
<link rel="stylesheet" href="styleDes.css" type="text/css" />
<![endif]-->

问题是,第二行被认为是虚假评论,同一行上的第二个标签被认为是评论的过早结束。

在同一行和第一个 endif 上添加额外的标签只会给我两个虚假的注释错误。

有什么方法可以让我的样式表带有条件,并让它们进行验证吗?或者我注定会拥有无效的代码,这些代码会困扰我的强迫症编码?


您的第二行的开头注释分隔符位于错误的位置:

<!--[if gte IE 9]><!-->

请注意此处的语法突出显示,它现在可以正确突出显示为注释。

接下来的其余标记也将正确突出显示,因为<!-->现在被视为<!其次是-->, 而不是<!--其次是>因为它会出现在您的无效标记中:

<link rel="stylesheet" href="Scripts/styleMain.css" media="all" type="text/css" />
<!--[if gte IE 9]><!-->
<link rel="stylesheet" href="Scripts/styleMob.css" media="screen and (max-width:500px)" type="text/css" />
<link rel="stylesheet" href="Scripts/styleDes.css" media="screen and (min-width:501px)" type="text/css" />
<!--<![endif]-->
<!--[if lt IE 9]>
<link rel="stylesheet" href="styleDes.css" type="text/css" />
<![endif]-->

代码中未突出显示为注释的部分将是 IE9 及更高版本以及其他浏览器将如何查看您的标记的方式。较旧的 IE 只会看到您的第一个和最后一个<link>元素。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为样式表创建有效的条件注释,不会出现“虚假注释”验证器错误 的相关文章