【问题标题】:Switch href url with javascript/jQuery for desktop to mobile使用 javascript/jQuery 将桌面的 href url 切换到移动设备
【发布时间】:2013-05-10 12:49:25
【问题描述】:

大家好,我似乎找不到我想要做的确切事情,作为一个 javascript 业余爱好者,我似乎无法解决它。 基本上... 我将此脚本附加到页面上的各种元素

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

});

我想做的是: 如果在移动设备上,则将 www.linkdesktop.com 切换到 www.linkmobile.com

这可能吗,我是根据屏幕尺寸还是使用某种移动检测脚本来做?

感谢解答,非常感谢。


好的,谢谢你的回答

所以也许是这样的?

    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');
});

}

});

【问题讨论】:

    标签: javascript jquery mobile


    【解决方案1】:

    在我的上一个项目中,我使用了这个solution to check mobile user。优雅而简约。

    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');
    

    【讨论】:

    • 好的,谢谢,如果我将您的方法与上面声明的变量一起使用,这应该可以工作:' $(document).ready(function() { if( isMobile.any() ) { $("h1 , h2, h5, .class1, .class2 #image1").click(function () { window.open('linkmobile.com'); }); } else { $("h1, h2, h5, .class1, . class2 #image1").click(function () { window.open('linkdesktop.com'); }); } }); '
    • 会的。但是要全局声明 isMobile var,我会将 isMobile 初始化和声明放入 $(document).ready(function(){ var isMobile = {...}; );
    【解决方案2】:

    您可以检查用户代理 (window.navigator.userAgent),请参阅 Determine if user navigated from mobile Safari

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多