简体   繁体   English

将javascript / jQuery的href网址切换到桌面设备到移动设备

[英]Switch href url with javascript/jQuery for desktop to mobile

Hi guys I can't seem to find the exact thing I am trying to do, and being a javascript amateur I can't seem to wrangle it. 嗨,大家好,我似乎找不到我想要做的确切的事情,而作为一名javascript业余爱好者,我似乎无法解决这个问题。 Basically... I have this script attached to various elements on my page 基本上...我将此脚本附加到页面上的各种元素上

$(document).ready(function() {
$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkdesktop.com');
});

}); });

and what I want to do is: IF on mobile device THEN switch www.linkdesktop.com TO www.linkmobile.com 我想做的是:如果在移动设备上,则将www.linkdesktop.com切换到www.linkmobile.com

Is this possible, do I do it based on screen size or using some sort of mobile detect script? 这有可能吗,我是根据屏幕尺寸还是使用某种移动检测脚本来做到这一点?

Answers appreciated, thanks a lot. 答案表示赞赏,非常感谢。


Ok so thanks for the answer 好,谢谢你的回答

so perhaps somthing like this? 所以也许这样的事?

    var userAgent = window.navigator.userAgent;

$(document).ready(function() {
if( (Android|webOS|iPhone|iPad|iPod|BlackBerry).test(navigator.userAgent) ) {

$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkmobile.com');
});

}
else {

$("h1, h2, h5, .class1, .class2 #image1").click(function () {
    window.open('https://www.linkdesktop.com');
});

}

});

In my last project I've used this solution to check mobile user . 在上一个项目中,我使用了此解决方案来检查移动用户 Elegant and simply. 优雅而简单。

var isMobile = {
  Android: function() {
    return navigator.userAgent.match(/Android/i);
  },
  BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i);
  },
  iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
  },
  Opera: function() {
    return navigator.userAgent.match(/Opera Mini/i);
  },
  Windows: function() {
    return navigator.userAgent.match(/IEMobile/i);
  },
  any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
  }
};

if( isMobile.any() ) alert('Mobile');

您可以检查用户代理(window.navigator.userAgent),请参阅确定用户是否从移动Safari浏览

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM