如何检测用户的浏览器并应用特定的CSS文件? [英] How do I detect the user’s browser and apply a specific CSS file?
问题描述
我需要检测浏览器并应用匹配的CSS文件。
我创建了3个css文件:__ie.css,ff.css,opera.css。现在我需要检测浏览器,以包括好的。
我知道这个
<! - [if IE]>
< link rel =stylesheethref =css / ie.csstype =text / css/>
<![endif] - >
但是如何在Firefox和Opera / Chrome上做同样的操作?
如果你必须检测浏览器只是应用CSS,那么你可能想重新思考你的CSS之前,浏览器特定的样式表。它所需要的是一个浏览器模仿另一个用户代理字符串,或一个新的版本被释放,一切都打破。使用目前的标准并验证您的代码( http://validator.w3.org/ ),您必须担心少得多的跨浏览器问题。即使只是使用< $ - [if IE]><![endif] - >
没有版本号可能会破坏更新版本的布局。 / p>
话虽如此,如果您想根据CSS功能的可用风格对网页进行不同的样式,请查看 Modernizr 。这样,您只能检查功能,如果发布新版本的浏览器,则不会损坏。
如果其他所有操作失败,您确实需要检测访问者的浏览器,请尝试 jquery.browser
。它内置jQuery,使用简单。 http://api.jquery.com/jQuery.browser/ 。
I need to detect the browser and apply the matched CSS file.
I have created 3 css files: __ie.css, ff.css, opera.css. Now I need to detect the browser in order to include the good one.
I know this
<!--[if IE]>
<link rel="stylesheet" href="css/ie.css" type="text/css"/>
<![endif]-->
But how do I do the same with Firefox and Opera/Chrome?
If you have to detect browsers just to apply CSS, then you might want to rethink your CSS before going to browser-specific stylesheets. All it takes is for one browser to mimic another's user agent string, or a new version to be released, and everything breaks. Use the current standards and validate your code (http://validator.w3.org/), and you'll have to worry about far fewer cross-browser issues. Even just using <!--[if IE]><![endif]-->
without a version number could break the layout in later versions.
That being said, if you want to style the page differently based on what CSS features are available, take a look at Modernizr. This way, you're only checking features, which won't be broken if a new version of the browser is released.
If all else fails and you really need to detect the visitor's browser, try jquery.browser
. It's built into jQuery, and is simple to use. http://api.jquery.com/jQuery.browser/.
这篇关于如何检测用户的浏览器并应用特定的CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!