Conditional Comments

我怕爱的太早我们不能终老 提交于 2019-12-29 10:00:14

问题


I am a newbie to UI. I see the conditional comments used to identify the IE browser like

<!--[if IE 10]> <html class="no-js ie10" lang="en"> <![endif]-->
<!--[if !IE]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

They work fine but can anyone explain how it works? Doesn't IE recognize these statements as comments?


回答1:


First of all, IE10 does not have Conditional Comments Recognition, it has been disabled, it works only for IE9 and lower. Versions over 10 are much less buggy than the earlier ones. This is how this works:

<!--[if lte IE8]>
  <style type="text/css">
    IE lower or equal to 8 CSS rules go here
  </style>
<![endif]-->

An entire stylesheet

<!--[if lte IE8]>
  <link rel="stylesheet" type="text/css" href="ie8-and-lower.css"/>
<![endif]-->

lower and equal to IE7

<!--[if lte IE7]>

lower than IE7

<!--[if lt IE7]>

greater than IE7

<!--[if gt IE7]>

not IE7

<!--[if !IE7]>

IE7

<!--[if IE7]>

Conditional comments do not work within stylesheets. But, you can use conditional comments in your HTML to apply different CSS classes or IDs to elements that you can then target with CSS.

Works something like this:

<!--[if IE7]>
  <div id="content" class="ie7">
<![endif]-->
<!--[if !IE7]>
  <div id="content">
<![endif]-->



回答2:


These are just conditional statements that are read by internet explorer, and allow web devs to get around the plethora of issues developing for IE brings.

Ironically, IE10 and above drops support for these entirely so they are really only useful for retro coding for old browsers, i,.e when using HTML5 shivs such as

<!--[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->

How do they work.. IE (below 10) just detects them as part of its rendering engine, and other browsers just treat as comments

Here some info about them being removed, and a link to other resources if you want to look more into it, or get better examples

http://www.therailsview.com/2012/02/internet-explorer-10-to-no-longer-support-conditional-comments/


来源:https://stackoverflow.com/questions/21613462/conditional-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!