为什么只有IE有条件注释?
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->
我是否缺少条件注释的其他用途,或者只有 IE 才能获取这些?
另外,为什么只有 IE 才能获取这些?
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->
Are there any other uses of conditional comments i'm missing or is it only IE that gets these?
Also, why is only IE that gets these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说得对;只有IE有这个功能。
这是微软在浏览器大战期间为 IE 发明的一项功能。它从未被批准为标准,也没有其他浏览器制造商实施过它。
他们将其作为一系列功能的一部分来实现,旨在让仅使用 IE 的开发人员的生活更轻松,以便他们能够确定他们正在使用哪个 IE 版本。这是必要的,因为 IE(与当时的其他浏览器相同)正在开发非标准功能,并且从一个版本到下一版本通常会发生极其重大的变化。
所以它是为 IE 开发人员设计的,但事实上它最终起到了相反的效果,因为它现在被跨浏览器开发人员普遍用作攻击其网站以使其与旧版本 IE 兼容的一种方式。
事实上,其他浏览器制造商都没有实现它可能是一件好事,因为虽然它确实使您很容易知道您所使用的浏览器版本,但浏览器/版本检测通常被认为是不好的做法。
当前的想法倾向于功能检测而不是浏览器/版本检测。为此,您可以使用 Modernizr 等工具来确定用户浏览器支持的功能,这可以为您提供更好的选择对如何处理旧浏览器的精细控制。
然而,对于旧版本的 IE,浏览器与某些标准严重不兼容,有时这还不够,在这种特定情况下确实需要检查浏览器和版本。鉴于此,条件评论功能的可用是一件幸事。鉴于没有其他浏览器存在此类问题,它们不支持条件注释可能同样是一件幸事。
在理想的世界中,没有人会继续使用需要这种破解的旧版 IE。但它们确实如此,我们需要支持它们,因此对于少数无法通过特征检测来做到这一点的情况,条件注释是可行的方法。
[编辑]
这是一个旧答案,但值得复活,因为从 IE10 开始,不再支持条件注释。 IE10 是一款比其前辈更好的浏览器,微软一直在积极努力使其尽可能现代化且符合标准。删除条件评论功能是该计划的一部分。当然,旧版本的条件注释仍然存在,并且上面支持和反对使用它们的论点仍然存在,但对于 IE10 和未来的版本,条件注释处理浏览器问题的选项不再可用。
You're right; only IE has this feature.
It is a feature that Microsoft invented for IE back in the days of the browser wars. It was never approved as a standard and no other browser maker ever implemented it.
They implemented it as part of a suite of features aimed at making life easier for IE-only developers so they could work out which IE version they were working in. This was necessary because IE (in common with other browsers at the time) was developing non-standard features and there were often extremely significant changes from one version to the next.
So it was intended for IE developers, but in fact it's ended up working to the opposite effect, as it is now commonly used by cross-browser developers as a way of hacking their site to make it work with older versions of IE.
In truth, it is probably a good thing that none of the other browser makers ever implemented it, because although it does indeed make it dead easy to know what version of the browser you're in, browser/version detection is generally considered to be poor practice.
Current thinking favours feature detection instead of browser/version detection. For this you use a tool such as Modernizr to determine the features that the user's browser supports, which gives you much finer grained control over how to deal with older browsers.
However, in the case of older versions of IE, the browser is so badly incompatible with some standards that this is sometimes not sufficient, and one does need to check for the browser and version in this specific case. Given this, it is a blessing that the conditional comment feature is available. And given that no other browser has such issues, it is probably just as much of a blessing that they don't support conditional comments.
In an ideal world, no-body would still be using such old versions of IE that require this kind of hack. But they are, and we need to support them, so for those few cases where you can't do it with feature detection, conditional comments are the way to go.
[EDIT]
This is an old answer, but it is worth resurecting because as of IE10, conditional comments are no longer supported. IE10 is a much much better browser than its predecessors, and Microsoft have been actively pushing to make it as modern and standards-compliant as possible. Removing the conditional comments feature is part of that push. Conditional comments remain in place for older versions of course, and the arguments above for and against using them remain, but for IE10 and future versions going forward, the option of conditional comments to deal with browser issues is no longer available.
微软实现了这些,以便开发人员可以利用较新浏览器的增强功能。但事实上,就像上面回答的那样,它们现在更多地用于确保在不同浏览器中运行的代码也可以在 IE 中运行。
完整故事:
http://msdn.microsoft.com/en -us/library/ms537512%28v=vs.85%29.aspx
Microsoft implemented these so developers could take advantage of enhanced features of newer browsers. But indeed, like answered above, they are now more used to make sure code that works in different browsers also work in IE.
Full story:
http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx
这是因为您需要条件注释来帮助 IE 呈现您的页面。否则,大多数页面将无法在 IE 中正确显示(因为没有正确处理默认值)。
That's because you need conditional comments to get help the IE to render your page. Otherwise most pages would not show correctly in IE (because of not handling defaults correctly).