search

Home  >  Q&A  >  body text

jquery - 如何针对手机浏览器和桌面浏览器分别执行不同的 javascript 语句?

比如有些语句想在桌面浏览器下执行,但不希望手机浏览器执行,有什么办法?

迷茫迷茫2902 days ago546

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-04-10 12:49:22

    参考: http://segmentfault.com/q/1010000000227004#a-1020000000267632

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-10 12:49:22

    整理于网络:

    //判断是否是手机访问;
    var ua = navigator.userAgent;
    var ipad = ua.match(/(iPad).*OSs([d_]+)/),
        isIphone = !ipad && ua.match(/(iPhonesOS)s([d_]+)/),
        isAndroid = ua.match(/(Android)s+([d.]+)/),
        isMobile = isIphone || isAndroid,
        isWeiXin = ua.match(/MicroMessenger/);
    if(isMobile || isWeiXin) {
    }else{
    }
    

    reply
    0
  • 阿神

    阿神2017-04-10 12:49:22

    判断ua

    navigator.userAgent返回的有一项是操作系统

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-10 12:49:22

    navigator.userAgent.toLowerCase().indexOf('mobile') > -1

    reply
    0
  • 迷茫

    迷茫2017-04-10 12:49:22

    var ua = window.navigator.userAgent.toLowerCase();
        if (ua.indexOf('android') != -1) {
            location.replace('android.html');
        } else if (ua.indexOf('iphone') != -1) {
            location.replace('iphone.html');
        }else{
            location.replace('pc.html');
        }
    
    

    reply
    0
  • Cancelreply