基于浏览器的替代样式表

时间:2011-06-15 06:10:27

标签: javascript css cross-browser

在那里的任何人都知道是否可以使用javascript来确定页面正在使用的浏览器并相应地加载样式表。理想情况下,我希望每个浏览器都有一个不同的sytlesheet,css媒体查询现在只能到目前为止。

3 个答案:

答案 0 :(得分:3)

如果您只想尝试IE版本,则条件评论效果很好!

http://www.quirksmode.org/css/condcom.html

如果您想针对这些功能定位浏览器,请使用Modernizr:

http://www.modernizr.com/

如果以上都不是您应该查看浏览器检测:

http://www.w3schools.com/js/js_browser.asp

答案 1 :(得分:0)

是的,它有可能。您可以通过“navigator”对象检测浏览器,但我想最好的选择是在detecting the browserdynamically loading a stylesheet使用jQuery。

答案 2 :(得分:0)

使用内置导航器对象的浏览器检测示例(来源:http://www.w3schools.com/js/js_browser.asp):

<div id="example"></div>

<script type="text/javascript">
  txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
  txt+= "<p>Browser Name: " + navigator.appName + "</p>";
  txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
  txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
  txt+= "<p>Platform: " + navigator.platform + "</p>";
  txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";

  document.getElementById("example").innerHTML=txt;
</script>

然后查看这个以了解如何使用JavaScript加载CSS:
How to load up CSS files using Javascript?

这也可能有效(来源:http://snippets.dzone.com/posts/show/4554):

function includeCSS(p_file) {
    var v_css  = document.createElement('link');
    v_css.rel = 'stylesheet'
    v_css.type = 'text/css';
    v_css.href = p_file;
    document.getElementsByTagName('head')[0].appendChild(v_css);
}
相关问题