[英]conditional comments in html
你能告诉我哪里错了吗? 我无法在任何浏览器中的条件注释中看到任何文本。
web.html
<!DOCTYPE html>
<html lang="en-US>
<head>
<meta charset="utf-8" />
<link href="style.css" type="text/css" rel="stylesheet" />
<head/>
<body>
<!--[if !IE]>
<p><span class="p-style">XXXXXXXXXXXX</span></p>
<![endif]-->
<!--[if IE]>
<p><span class="p-style-IE">YYYYYYYYYYYYY</span></p>
<![endif]-->
</body>
</html>
style.css
.p-style {
color:red;
}
.p-style-IE {
color:green;
}
谢谢。
IE以外的浏览器将条件语句视为注释,因为它们被包含在注释标记内。
<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->
但是,当您定位的浏览器不是IE时,则必须使用2条注释,一条在代码之前,一条在代码之后。 IE将忽略它们之间的代码,而其他浏览器会将其视为普通代码。 因此,针对非IE浏览器的语法为:
<!--[if !IE]-->
IE ignores this
<!--[endif]-->
您的代码有一些问题,我已予以纠正。 使用IE9和其他浏览器进行检查。 现在,它的工作正常,但在IE10及更高版本中除外(因为IE10及更高版本不再支持条件标记)
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="utf-8" /> <link href="style.css" type="text/css" rel="stylesheet" /> <head/> <body> <p><span class="p-style">XXXXXXXXXXXX</span></p> <p><span class="p-style-IE">YYYYYYYYYYYYY</span></p> <!--[if IE]> <style> .p-style {color:red;} .p-style-IE {display: none;} </style> <![endif]--> <!--[if !IE]><!--> <style> .p-style {display: none;} .p-style-IE{color:green;} </style> <!--<![endif]--> </body> </html>
更新:对于IE10和IE11,
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
/* IE10+ specific styles go here */
}
我们使用-ms-high-contrast创建一个媒体查询,您可以在其中放置IE10 +特定的CSS样式。 因为-ms-high-contrast是特定于Microsoft的(并且仅在IE10 +中可用),所以只能在Internet Explorer 10及更高版本中对其进行解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.