jquery - 检查 iOS 版本是否 >= 4.2 并根据结果修改 CSS

标签 jquery css ios mobile user-agent

我正在使用 Typekit.com 在网站上显示 cufonts,但是,它仅与 >= 4.2 iOS 的移动浏览器兼容。我已经使用 jQuery 来测试访问者是否使用 iOS 并根据过去的内容显示特定内容(不费吹灰之力),但我在弄清楚如何更具体并仅修改 CSS 时遇到了很多麻烦如果版本低于 iOS 4.2 版。似乎无法弄清楚...有什么帮助吗?

最佳答案

将您的 4.2+ 样式放入一个样式表中。将您的 <4.2 样式放入另一个样式。检查版本并将适当的样式表添加到 <head> .您需要测试较旧的用户代理字符串,但这应该适用于大多数情况。该函数返回 0 (如果它无法确定或者它不是 iOS)作为“旧”样式表的后备。

演示:http://jsfiddle.net/ThinkingStiff/LJrEW/

脚本:

var style = '';

if( getOsVersion() >= 4.2 ) {
    style = '<link rel="stylesheet" href="styles/ios.css" />';
} else {
    style = '<link rel="stylesheet" href="styles/ios_old.css" />';
};

document.head.insertAdjacentHTML( 'beforeEnd', style );

function getOsVersion() {
    var agent = window.navigator.userAgent,
        start = agent.indexOf( 'OS ' );

    if ( /iphone|ipod|ipad/.test( agent ) && start > -1 ) {
        return window.Number( agent.substr( start + 3, 3 ).replace( '_', '.' ) );
    } else {
        return 0;
    };

};

关于jquery - 检查 iOS 版本是否 >= 4.2 并根据结果修改 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8733073/

相关文章:

ios - "The PayPal iOS SDK is currently available in the US",具体是什么意思?

ios - 适用于 iPhone 和 iPad 的动态 xib 布局

javascript - 如何访问 json php 中的数组到 javascript/jquery

javascript - jQuery 未在 Electron 应用程序的 web View 中加载

javascript - 如何根据从 cookie 检索的值来选择选择框值

jquery - 如何设置图像相对于它所在的 DIV 的不透明度

html - 如何在 CSS 中悬停时显示隐藏内容

Html Css 水平无序列表 25px 左右空间顶部和底部?

ios - UIimageView 蒙版成菱形

javascript - jQuery Sortable - 允许将某些项目仅放入某些列表中