CSS 的浏览器检测:使用单独的 CSS 文件或内联 CSS 或...?

发布于 2024-07-12 05:13:29 字数 732 浏览 12 评论 0原文

我想知道哪种速度最好:

选项 1:为每个浏览器加载单独的 CSS(通过

// for internet explorer 6 & 7
if ($.browser.msie) {
    document.write('<link rel="stylesheet" type="text/css" href="/css/styles-ie.css" />');
}

// for opera
if ($.browser.opera) {
    document.write('<link rel="stylesheet" type="text/css" href="/css/styles-opera.css" />');
}

选项 2:内联 CSS(单独的 JS 文件)通过

// for internet explorer 6 & 7
if ($.browser.msie) {
    document.write('[some CSS styles]');
}

// for opera
if ($.browser.opera) {
    document.write('[some CSS styles]');
}

选项 3:或者还有其他更好的方法吗?

I'm wondering which is best for speed:

Option 1: Loading separate CSS for each browser (separate JS file included through a <script/>):

// for internet explorer 6 & 7
if ($.browser.msie) {
    document.write('<link rel="stylesheet" type="text/css" href="/css/styles-ie.css" />');
}

// for opera
if ($.browser.opera) {
    document.write('<link rel="stylesheet" type="text/css" href="/css/styles-opera.css" />');
}

Option 2: Inline CSS (separate JS file included through a <script/>):

// for internet explorer 6 & 7
if ($.browser.msie) {
    document.write('[some CSS styles]');
}

// for opera
if ($.browser.opera) {
    document.write('[some CSS styles]');
}

Option 3: Or is there another better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-07-19 05:13:29

我最喜欢 条件注释,虽然我没有真正测试它,但我希望它也是最快的选项,因为不涉及 DOM 操作。 还有一篇关于支持 IE 对话的很棒的文章使用这种技术,但阅读有关某些 陷阱也是如此。

根据我的经验,除了 IE 之外,没有必要对其他浏览器做出任何例外,但由于您的示例也列出了 Opera,我建议您使用一些 CSS hack 来进行 Opera 所需的(可能很小)更改。 如果您在网上搜索 Opera Specific CSS,您可能会找到您喜欢的东西;)

I like conditional comments best, and while I didn't really test it, I expect it to be the fastest option as well, since no DOM manipulation is involved. There's also a great article about supporting IE talking with this technique, but it might be helpful to read an article on some pitfalls as well.

In my experience, it's not necessary to make any exception for other browsers than IE, but since your example lists Opera as well, I'd suggest you use some CSS hacks to make the (probably tiny) changes you need for Opera. If you do a web search for Opera Specific CSS you'll probably find something that you like ;)

惯饮孤独 2024-07-19 05:13:29

我使用 CSS Reset 来稍微平衡一下竞争环境。 这通常会使所有浏览器的行为与我应用的样式相当可预测。 当然,我遇到过一些情况,IE 的渲染方式与更符合标准的浏览器不同。 使用 条件注释 我可以使用 IE 特定的样式表来修复这些差异。

I use a CSS Reset to level the playing field a bit. This usually makes all browsers behave rather predictably to the styles I apply. Of course, I have run into a few instances where IE renders things differently from more standards compliant browsers. Using conditional comments I can fix these differences with an IE specific stylesheet.

浮光之海 2024-07-19 05:13:29

您可以使用

  • IE 条件注释
  • CSS 规则特定
  • 条件 HTML 类

来检测 IE 浏览器这里很棒 article 显示所有规则

You can detect IE Browser either using

  • IE Conditional Comments
  • CSS Rules Specific
  • Conditional HTML Class

Here is great article which show all of the rules

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文