5

I have the following in my head tag:

<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]-->

Problem is, the second line is considered a bogus comment, and the second tag on the same line is considered a premature end of comment.

Having the extra tags on that same line, and on the first endif just gives me two bogus comment errors.

Is there any way I can have my stylesheets with conditionals, and get them to validate? Or am I doomed to have invalid code that would nag at my OCD coding?

4

2 回答 2

7

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

<!--[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>元素。

于 2013-07-05T15:14:49.003 回答
0

这应该有效。

<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]-->
于 2013-07-05T15:08:41.990 回答