[英]Add CSS classes targeting IE browsers: JavaScript or Conditional Comments?
My goal is to remove IE conditional comments from my documents' <head>
, but I recognize that I still need to use ie6,7,8-specific stylesheets (correct me if I'm wrong). 我的目标是从文档的
<head>
删除IE条件注释,但是我意识到我仍然需要使用特定于ie6,7,8的样式表(如果我输入错了,请更正我)。
For example, these IE conditionals are used by HTML5Boilerplate 3.0 : 例如, HTML5Boilerplate 3.0使用以下IE条件:
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"><![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
So instead I'm thinking that I'd like to use JavaScript to add classes to my html tag resulting in this ( as illustrated here ): 因此,相反,我认为我想使用JavaScript将类添加到html标记中,从而导致此结果( 如此处所示 ):
<html lang="en" class="ie6 ie7 ie8">
So my question: What are the drawbacks (consequences) of using JavaScript to add CSS classes to my documents' html element versus using IE conditional comments? 所以我的问题是:与使用IE条件注释相比,使用JavaScript向文档的html元素中添加CSS类的缺点(后果)是什么?
One effect I can think of right now: 我现在可以想到的一种效果是:
EDIT: I'm going to go ahead and concede that the most reliable way to add those classes is (sadly) to use IE conditional comments. 编辑:我要继续承认,添加这些类的最可靠方法是(严重)使用IE条件注释。 Thanks all for the dialog.
谢谢大家的对话。
Another thing is, JavaScript alone is not a good idea to perfectly detect the browsers. 另一件事是,单独使用JavaScript并不是完美检测浏览器的好主意。 Have you heard of User Agent Spoofing ?
您听说过用户代理欺骗吗? JavaScript detects a browser by User Agents.
JavaScript通过用户代理检测浏览器。 But, if a browser comes with a spoofed User Agent, then Firefox can act like Chrome.
但是,如果浏览器随附了欺骗性的用户代理,则Firefox的行为类似于Chrome。 Hope you get it!
希望你能得到!
From the same link you have given, 从您给出的相同链接中 ,
- You don't load the rules that won't be applying because they're not for this browser.
您不会加载不适用的规则,因为它们不适用于此浏览器。
- You don't depend on a technology that can be turned off independently of CSS.
您不依赖可以独立于CSS关闭的技术。
- You don't depend on a technology where a syntax error could temporarily break your browser detection.
您不依赖语法错误可能会暂时中断浏览器检测的技术。
- There's no sense in wasting browser time setting the relevant class and recalculating the styles everywhere.
浪费浏览器时间来设置相关的类并重新计算各处的样式没有任何意义。 Yes, this time is essentially unnoticeable, but from a 'dirty vs clean' stand point I think that make it dirtier.
是的,这一次基本上是不明显的,但是从“肮脏还是干净”的角度来看,我认为这会使它更脏。
I personally think , you shouldnt rely on sniffing userAgent
and should not load the unnecessary css rules for ie
only. 我个人认为,您不应该依赖于嗅探
userAgent
并且不应仅为ie
加载不必要的css规则。
Why can't you add a seperate style sheet with IE specific design for the required classes instead of adding more classes to the html element. 为什么不能为必需的类添加具有IE特定设计的单独样式表,而不是向html元素添加更多类。 You can also refer http://paulirish.com/2009/browser-specific-css-hacks/ where in one css file you can target all the browsers with the css hacks.
您也可以参考http://paulirish.com/2009/browser-specific-css-hacks/ ,在其中一个CSS文件中,您可以将所有具有CSS hacks的浏览器作为目标。 This would also solve your initial purpose of removing the IE specific condition from the html head tag
这也可以解决您从html头标记中删除IE特定条件的最初目的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.