当前位置: 首页 > news >正文

在JavaScript / HTML中,浏览器中如何使用js判断是否为苹果平台

苹果平台检测器

下面是一个简洁的JavaScript函数,可以检测当前是否为苹果平台,并提供一个直观的演示界面:

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>苹果平台检测器</title><style>body {font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);margin: 0;padding: 20px;display: flex;flex-direction: column;align-items: center;justify-content: center;min-height: 100vh;color: #333;}.container {background: white;border-radius: 16px;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);padding: 30px;max-width: 500px;width: 90%;text-align: center;}h1 {color: #333;margin-bottom: 10px;}.subtitle {color: #666;margin-bottom: 30px;}.result {font-size: 1.5rem;font-weight: 600;padding: 20px;border-radius: 12px;margin: 25px 0;transition: all 0.3s ease;}.is-apple {background: #e8f5e9;color: #2e7d32;border: 2px solid #4caf50;}.not-apple {background: #ffebee;color: #c62828;border: 2px solid #f44336;}.code-container {background: #2d2d2d;color: #f8f8f2;padding: 20px;border-radius: 8px;text-align: left;margin: 20px 0;overflow-x: auto;font-family: 'Courier New', monospace;}.info {background: #e3f2fd;padding: 15px;border-radius: 8px;margin-top: 20px;text-align: left;font-size: 0.9rem;}.device-icon {font-size: 4rem;margin: 20px 0;}</style>
</head>
<body><div class="container"><h1>苹果平台检测器</h1><p class="subtitle">检测当前浏览器是否运行在苹果设备上</p><div class="device-icon" id="device-icon">📱</div><div id="result" class="result">检测中...</div><div class="code-container">
// 检测是否为苹果平台的函数
function isApplePlatform() {const platform = navigator.platform.toLowerCase();const userAgent = navigator.userAgent.toLowerCase();// 检测平台标识if (platform.includes('mac') || platform.includes('iphone') || platform.includes('ipad') || platform.includes('ipod')) {return true;}// 检测用户代理字符串if (userAgent.includes('mac os') || userAgent.includes('iphone') || userAgent.includes('ipad') || userAgent.includes('ipod')) {return true;}return false;
}</div><div class="info"><p><strong>检测原理:</strong>通过检查 navigator.platform 和 navigator.userAgent 是否包含苹果设备的关键词。</p><p><strong>支持的设备:</strong>Mac、iPhone、iPad、iPod</p></div></div><script>// 检测是否为苹果平台的函数function isApplePlatform() {const platform = navigator.platform.toLowerCase();const userAgent = navigator.userAgent.toLowerCase();// 检测平台标识if (platform.includes('mac') || platform.includes('iphone') || platform.includes('ipad') || platform.includes('ipod')) {return true;}// 检测用户代理字符串if (userAgent.includes('mac os') || userAgent.includes('iphone') || userAgent.includes('ipad') || userAgent.includes('ipod')) {return true;}return false;}// 更新页面显示结果function updateResult() {const resultElement = document.getElementById('result');const iconElement = document.getElementById('device-icon');const isApple = isApplePlatform();if (isApple) {resultElement.textContent = "✅ 当前设备是苹果平台";resultElement.className = "result is-apple";iconElement.textContent = "🍎";} else {resultElement.textContent = "❌ 当前设备不是苹果平台";resultElement.className = "result not-apple";iconElement.textContent = "💻";}}// 页面加载完成后更新结果document.addEventListener('DOMContentLoaded', updateResult);</script>
</body>
</html>

核心函数说明

上面代码中的 isApplePlatform() 函数通过以下方式检测苹果平台:

  1. 检查 navigator.platform - 这个属性直接返回平台信息
  2. 检查 navigator.userAgent - 这个属性包含浏览器和设备的完整信息

函数会检测以下关键词:

  • “mac” (Mac电脑)
  • “iphone” (iPhone)
  • “ipad” (iPad)
  • “ipod” (iPod)

如果找到任何这些关键词,函数返回 true,否则返回 false

您可以直接复制这个HTML文件并在任何浏览器中打开使用,它会自动检测并显示当前是否为苹果平台。

http://www.dtcms.com/a/491501.html

相关文章:

  • 磁盘-IO
  • Spring Boot 外部化配置最佳实践指南
  • 提升网站访问速度一家做特卖的网站手机版
  • 国外有什么网站是做服装的wordpress修改管理密码
  • [linux仓库]信号保存[进程信号·肆]
  • AI产品经理学习笔记3 - Agent认知与原理分析
  • 建站代理赚钱吗做企业网站需要提供什么
  • 网站开发先写什么后写什么wordpress屏蔽功能org
  • RFSOC47DR+CPU 6U VPX射频信号处理板
  • 厦门网站建设缑阳建wordpress公告模板
  • debug - MDK - arm-none-eabi - 同样的工具链,源码,编译参数,elf是一样的
  • 前端-配置Prettier与ESLint9
  • 中企动力网站建设公司百度指数下载手机版
  • 蚂蚁 S21 XP+ HYD 500T矿机评测:SHA-256算法与高效液冷系统
  • 蚂蚁 S19 XP Hyd 3U 512T矿机评测:高效水冷设计,适合BTC/BCH挖矿
  • 【Win32 多线程程序设计基础第四章笔记】
  • 2024.6卷一阅读短语
  • 企业营销推广型网站建设怎么创造软件app
  • Rust 的错误处理:别拿类型系统当护身符
  • 用栈实现记忆存储——C++语言自制时间计算器
  • 实验二 呼吸灯功能实验
  • 动力 网站建设珠海专业网站建设费用
  • 博客系统测试
  • 高德地图电子围栏/地图选区/地图打点
  • 自己动手建设网站过程dede珠宝商城网站源码
  • Git的分支
  • 基础拓展
  • 手机微网站建设河南网站建设的详细策划
  • 剧本杀小程序系统开发:内容生态与商业模式的双轮驱动
  • 网站备案表不会写引流网站怎么做