学习写法

1)定义判断设备的方法,注意返回值的使用

function getUserAgent() {
        var userAgent = navigator.userAgent,
            iPhone = userAgent.indexOf('iPhone') > -1,
            iPad = userAgent.indexOf('iPad') > -1,
            iPod = userAgent.indexOf('iPod') > -1,
            Android = userAgent.indexOf('Android') > -1;
        if (iPhone || iPad || iPod) {
            return {
                type: 'iOS'
            };
        } else if (Android) {
            return {
                type: 'Android'
            };
        } else {
            return {
                type: ''
            };
        }
    }

2)方法使用,先运行,取到返回值,方便下面直接使用

var userAgent = getUserAgent();

3)使用  userAgent.type 使用返回值

if(userAgent.type == 'iOS'){
    alert('苹果设备');
}else{
    alert('安卓及其他设备');  
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
  • 2021-07-05
  • 2021-12-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
相关资源
相似解决方案